Sort text with numbers in javascript

Asked

Viewed 97 times

-1

Guys I got this situation.

After mounting an array, with area and value information:

Area 9 - 45, Area 8 - 70, Area 7 - 100, Area 5 - 75, Area 3 - 110, Area 2 - 145, Area 14 - 20, Area 11 - 60, Area 10 - 70, Area 1 - 40

I need you to stay on that payroll at the highest value, regardless of the Area, but the Area information should stay with the value.

Area 2 - 145, Area 3 - 110, Area 7 - 100, Area 5 - 75, Area 10 - 70, Area 8 - 70, Area 11 - 60, Area 9 - 45, Area 1 - 40, Area 14 - 20

Here is the code that returns the first information:

function InserirSomatoriaArea(){




    var filtro1 = "?$filter=(Title%20ne%20'')%20and%20(Pontuacao%20ne%20'')"
    var dado1 = GetListItems('600862F-1E0E-4442-A95B-96235C460CA0', filtro1);

    var valor1=[];

    var filtroarea = "?$filter=(Title%20ne%20'')"
    var dado2 = GetListItems('600286F-1E0E-4442-A95B-96235C460CA0', filtroarea);

    var valorarea=[];
    var valorareaT=[];

    for (var it = 0; it < dado2.length; it++) {

        valorarea[it]=dado2[it].Title;

    }

    valorarea.sort(sortfunction); 

    function sortfunction(a, b){
        if (a < b) return -1; 
        if (a > b) return 1;
    }

    var arr_unique = [...new Set(valorarea)];


    //alert(arr_unique)

    var somaarea = 0;


    for (var it = 0; it < arr_unique.length; it++) {

        for (var itt = 0; itt < dado1.length; itt++) {

            if (arr_unique[it] == dado2[itt].Title){

            somaarea += Number(dado2[itt].Pontuacao);   


            }
        }

        MatrizSArea[it] = arr_unique[it] + " - " + somaarea ;
        MatrizSArea.sort(sortfunction1)
        somaarea=0;

    }
    alert (MatrizSArea)
    function sortfunction1(a, b){

        if (a < b) return 1;
        //if (a > b) return 1;
    }


}
  • Your question is a little vague. I see several sorts by code out. Not working ? What does the code itself ? Which array do you want to sort ? How this information in this array ?

1 answer

-1


You can do the ordering by calling the function Sort() as follows:

fruits.sort(function(a, b){return Number(a.substring(a.indexOf('-')+1, a.length).trim()) < Number(b.substring(b.indexOf('-')+1, b.length).trim())})
  • This is an example of sorting only change the name of the array that should work correctly, ordering decreasing by the value. As shown by the following https://www.w3schools.com/code/tryit.asp?filename=FPN1TUEJWN6N

Browser other questions tagged

You are not signed in. Login or sign up in order to post.