简体   繁体   English

javascript-按多个条件排序-首先按另一个数组,然后按内部的另一个属性

[英]javascript - sort by multiple criteria - first by another array and then by another property inside

I have an array of objects as below and need to sort it in such fashion that items with c comes first, then y , then s. 我有一个如下所示的对象数组,需要按以下方式对它进行排序:c项首先出现,y则是s。 And in that too, for eg in c, it should be sorted by counter ie c- counter 1, c- counter 2. 同样,例如在c中,它也应按计数器排序,即c-counter 1,c-counter 2。

var data = [ { age:7, counter: 1, code: 'c'},
     { age:5, counter: 2, code: 'c'},
     { age:4, counter: 3, code: 'c'},
     { age:19, counter: 2, code: 'y'},
     { age:22, counter: 1, code: 'y'},
     { age:57, counter: 1, code: 's'},
     { age:80, counter: 2, code: 's'}
    ]

Upon looking on SO, I was able to sort by c, y, s as below, but could not do another sort for counter inside. 在查看SO之后,我可以按以下方式按c,y,s进行排序,但是无法对内部计数器进行另一种排序。 How can I do that. 我怎样才能做到这一点。

var order =[c,y,s];
data.sort(function(a,b){
        return order.indexOf(a.code) < order.indexOf(b.code) ? -1:1;
})

You could use an object for the order of code and then sort by count. 您可以将对象用于code顺序,然后按计数排序。

 var data = [{ age: 7, counter: 1, code: 'c' }, { age: 5, counter: 2, code: 'c' }, { age: 4, counter: 3, code: 'c' }, { age: 19, counter: 2, code: 'y' }, { age: 22, counter: 1, code: 'y' }, { age: 57, counter: 1, code: 's' }, { age: 80, counter: 2, code: 's' }]; data.sort(function (a, b) { var order = { c: 1, y: 2, s: 3 }; return order[a.code] - order[b.code] || a.counter - b.counter; }); console.log(data); 

You need to specify array as 您需要将数组指定为

var order =['c', 'y', 's'];

otherwise c, y and s needs to be variables. 否则,c,y和s必须为变量。

Also, you need to compare the counter variables as well if codes are equal 另外,如果代码相等,则还需要比较计数器变量

data.sort(function(a,b){
   if ( a.code == b.code )
   {
        return a.counter - b.counter;
   }
   else
   {
       return order.indexOf(a.code) - order.indexOf(b.code);
   }
})

This is a simple way to sort by counter: 这是一种按计数器排序的简单方法:

var order = [ 'c', 'y', 's' ];

data.sort(function (a,b) {
    var o;

    if (order.indexOf(a.code) === order.indexOf(b.code))
    {
        o = a.counter - b.counter;
    } else {
        o = order.indexOf(a.code) - order.indexOf(b.code);
    }

    return o;
});

This is a dynamic way which allows you to add your conditions to be compare one after another. 这是一种动态方式,可让您添加条件以进行比较。

 var data = [{ age: 7, counter: 1, code: 'c' }, { age: 5, counter: 2, code: 'c' }, { age: 4, counter: 3, code: 'c' }, { age: 19, counter: 2, code: 'y' }, { age: 22, counter: 1, code: 'y' }, { age: 57, counter: 1, code: 's' }, { age: 80, counter: 2, code: 's' }]; Array.prototype.orderBy = function(...expresions) { var arr = this; return this.sort((a, b) => { var result; expresions.every(ex => (result = ex(a) - ex(b)) === 0) return result; }); } var codeOrder = { c: 1, y: 2, s: 3 }; var result = data .orderBy(i => codeOrder[i.code], i => i.counter) console.log(result); 

How it works 这个怎么运作

The usage is simple: 用法很简单:

dataArray.sortBy(ex1[, ex2[, ex3]])

every arguments of this method is a function which accepts one item from dataArray as input and returns a number related to that item. 此方法的每个参数都是一个函数,该函数接受dataArray一项作为输入并返回与该项有关的数字。 if any of expressions return same value with respect to adjacent item then evaluation continues with next expression on item and adjecent until they return different result or there is no other expression to test. 如果任何一个表达式相对于相邻项返回相同的值,则评估将继续在该项和相邻项上使用下一个表达式,直到它们返回不同的结果或没有其他要测试的表达式为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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