简体   繁体   中英

Sort Method Not Working Properly For Large number of elements

I have an array of arrays consisting of around 20 values as below,

var myArray = [
            ["2017-01", "Female", "671950.00"],
            ["2017-01", "Male", "322800.00"],
            ["2017-02", "Male", "323977.00"],
            ["2017-02", "Female", "671375.00"],
            ["2017-03", "Male", "326515.00"],
            ["2017-03", "Female", "683646.00"],
            ["2017-04", "Male", "326642.00"],
            ["2017-04", "Female", "684820.00"],
            ["2017-05", "Female", "687627.00"],
            ["2017-05", "Male", "330966.00"],
            ["2017-06", "Male", "330290.00"],
            ["2017-06", "Female", "686022.00"],
            ["2017-07", "Male", "326663.00"],
            ["2017-07", "Female", "679277.00"],
            ["2017-08", "Female", "684451.00"],
            ["2017-08", "Male", "329804.00"],
            ["2017-09", "Female", "686584.00"],
            ["2017-09", "Male", "330300.00"],
            ["2017-10", "Female", "658154.00"],
            ["2017-10", "Male", "316671.00"]
          ];

I am grouping the above code with the first element of array also the second element of array is sorted with respect to the group. For this I have written the below code,but this works fine for only ten elements in array as shown below,

var myArray = [
            ["2017-01", "Female", "671950.00"],
            ["2017-01", "Male", "322800.00"],
            ["2017-02", "Male", "323977.00"],
            ["2017-02", "Female", "671375.00"],
            ["2017-03", "Male", "326515.00"],
            ["2017-03", "Female", "683646.00"],
            ["2017-04", "Male", "326642.00"],
            ["2017-04", "Female", "684820.00"],
            ["2017-05", "Female", "687627.00"],
            ["2017-05", "Male", "330966.00"],

          ];

          myArray  = myArray.sort(function (a, b) {  

             return a[0] == b[0] && a[1] > b[1];

        });

If I use the previous array with 20 elements, it shows weird result.

You can use localeCompare() to compare string

 var myArray = [ ["2017-01", "Female", "671950.00"], ["2017-01", "Male", "322800.00"], ["2017-02", "Male", "323977.00"], ["2017-02", "Female", "671375.00"], ["2017-03", "Male", "326515.00"], ["2017-03", "Female", "683646.00"], ["2017-04", "Male", "326642.00"], ["2017-04", "Female", "684820.00"], ["2017-05", "Female", "687627.00"], ["2017-05", "Male", "330966.00"], ["2017-06", "Male", "330290.00"], ["2017-06", "Female", "686022.00"], ["2017-07", "Male", "326663.00"], ["2017-07", "Female", "679277.00"], ["2017-08", "Female", "684451.00"], ["2017-08", "Male", "329804.00"], ["2017-09", "Female", "686584.00"], ["2017-09", "Male", "330300.00"], ["2017-10", "Female", "658154.00"], ["2017-10", "Male", "316671.00"] ]; myArray.sort((a, b) => { if (a[0] !== b[0]) return a[0].localeCompare(b[0]); //If first element is not the same, compare the first element return a[1].localeCompare(b[1]); //Compare the second element since the first element is the same }); console.log(myArray); 

Doc: localeCompare()

Change the sorting logic by below and see it will work perfectly.

var sortedArray  = myArray.sort(function (a, b) {  
             return a > b ? 1 : -1;
        });

See Demo: JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM