How do you sort an array on multiple columns?

前端 未结 16 1102
面向向阳花
面向向阳花 2020-11-22 13:02

I have a multidimensional array. The primary array is an array of

[publicationID][publication_name][ownderID][owner_name] 

What I am tryin

相关标签:
16条回答
  • 2020-11-22 13:38

    If owner names differ, sort by them. Otherwise, use publication name for tiebreaker.

    function mysortfunction(a, b) {
    
      var o1 = a[3].toLowerCase();
      var o2 = b[3].toLowerCase();
    
      var p1 = a[1].toLowerCase();
      var p2 = b[1].toLowerCase();
    
      if (o1 < o2) return -1;
      if (o1 > o2) return 1;
      if (p1 < p2) return -1;
      if (p1 > p2) return 1;
      return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 13:41

    I suggest to use a built in comparer and chain the wanted sort order with logical or ||.

    function customSort(a, b) {
        return a[3].localeCompare(b[3]) || a[1].localeCompare(b[1]);
    }
    

    Working example:

    var array = [
        [0, 'Aluminium', 0, 'Francis'],
        [1, 'Argon', 1, 'Ada'],
        [2, 'Brom', 2, 'John'],
        [3, 'Cadmium', 3, 'Marie'],
        [4, 'Fluor', 3, 'Marie'],
        [5, 'Gold', 1, 'Ada'],
        [6, 'Kupfer', 4, 'Ines'],
        [7, 'Krypton', 4, 'Joe'],
        [8, 'Sauerstoff', 3, 'Marie'],
        [9, 'Zink', 5, 'Max']
    ];
    
    array.sort(function (a, b) {
        return a[3].localeCompare(b[3]) || a[1].localeCompare(b[1]);
    });
    
    document.write('<pre>');
    array.forEach(function (a) {
        document.write(JSON.stringify(a) + '<br>');
    });

    0 讨论(0)
  • 2020-11-22 13:46
    function multiSort() {
    
        var args =$.makeArray( arguments ),
            sortOrder=1, prop='', aa='',  b='';
    
        return function (a, b) {
    
           for (var i=0; i<args.length; i++){
    
             if(args[i][0]==='-'){
                prop=args[i].substr(1)
                sortOrder=-1
             }
             else{sortOrder=1; prop=args[i]}
    
             aa = a[prop].toLowerCase()
             bb = b[prop].toLowerCase()
    
             if (aa < bb) return -1 * sortOrder;
             if (aa > bb) return 1 * sortOrder;
    
           }
    
           return 0
        }
    
    }
    empArray.sort(multiSort( 'lastname','firstname')) Reverse with '-lastname'
    
    0 讨论(0)
  • 2020-11-22 13:49

    I think what you're looking for is thenBy.js: https://github.com/Teun/thenBy.js

    It allows you to use the standard Array.sort, but with firstBy().thenBy().thenBy() style.

    An example can be seen here.

    0 讨论(0)
  • 2020-11-22 13:50

    This is handy for alpha sorts of all sizes. Pass it the indexes you want to sort by, in order, as arguments.

    Array.prototype.deepSortAlpha= function(){
        var itm, L=arguments.length, order=arguments;
    
        var alphaSort= function(a, b){
            a= a.toLowerCase();
            b= b.toLowerCase();
            if(a== b) return 0;
            return a> b? 1:-1;
        }
        if(!L) return this.sort(alphaSort);
    
        this.sort(function(a, b){
            var tem= 0,  indx=0;
            while(tem==0 && indx<L){
                itm=order[indx];
                tem= alphaSort(a[itm], b[itm]); 
                indx+=1;        
            }
            return tem;
        });
        return this;
    }
    
    var arr= [[ "Nilesh","Karmshil"], ["Pranjal","Deka"], ["Susants","Ghosh"],
    ["Shiv","Shankar"], ["Javid","Ghosh"], ["Shaher","Banu"], ["Javid","Rashid"]];
    
    arr.deepSortAlpha(1,0);
    
    0 讨论(0)
  • 2020-11-22 13:50

    I found multisotr. This is simple, powerfull and small library for multiple sorting. I was need to sort an array of objects with dynamics sorting criteria:

    const criteria = ['name', 'speciality']
    const data = [
      { name: 'Mike', speciality: 'JS', age: 22 },
      { name: 'Tom', speciality: 'Java', age: 30 },
      { name: 'Mike', speciality: 'PHP', age: 40 },
      { name: 'Abby', speciality: 'Design', age: 20 },
    ]
    
    const sorted = multisort(data, criteria)
    
    console.log(sorted)
    <script src="https://cdn.rawgit.com/peterkhayes/multisort/master/multisort.js"></script>

    This library more mutch powerful, that was my case. Try it.

    0 讨论(0)
提交回复
热议问题