Sort text and numbers

-1

People have 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 in this order for the highest value, without taking into account the Area, but the Area information should be 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;
    }


}
    
asked by anonymous 22.03.2018 / 21:54

1 answer

-1

You can sort by calling the sort () function 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())})
    
22.03.2018 / 22:08