2012-07-07 75 views
9

数组我发现了一个伟大的方法进行排序基于属性的一个对象数组作为定义:如何排序多个字段值的对象在JavaScript

Sort array of objects by string property value in JavaScript

使用该功能如何工作完全适用于单一排序(在所有浏览器上),甚至可以在另一种排序内排序,除了使用谷歌浏览器!这里是爱琴奥兹坎对对象

function dynamicSort(property) { 
    return function (a,b) { 
     return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; 
    } 
} 

使用名为“数据”数组的数组大排序过程(当然,我的阵列有更多对象对)...

var Data = [{Category: "Business", Value: "ABC"},{Category:"Personal", Value:"XYZ"}]; 

我能得到如该命令是做这个列为每个类别中的所有值的正确排序...

Data.sort(dynamicSort("Value")); 
Data.sort(dynamicSort("Category")); 

,首先分拣上Value,然后通过Category,我的数组按排序顺序排列所有值,首先列出所有基于业务的值,然后列出所有基于个人的值。完善!除了Chrome中的数据按类别正确排序外,但每个类别中值的顺序似乎相当随机。

是否有人知道更好的方法来进行排序,也可以在Chrome中工作?

+2

由A排序,然后独立地由乙_is不一样排序排序由A **和** B_。如果它在某些浏览器上工作,那就是侥幸。 – Alnitak 2012-07-08 21:54:25

回答

35

我创建的dynamicSort功能的多参数版本:

function dynamicSort(property) { 
    return function (obj1,obj2) { 
     return obj1[property] > obj2[property] ? 1 
      : obj1[property] < obj2[property] ? -1 : 0; 
    } 
} 

function dynamicSortMultiple() { 
    /* 
    * save the arguments object as it will be overwritten 
    * note that arguments object is an array-like object 
    * consisting of the names of the properties to sort by 
    */ 
    var props = arguments; 
    return function (obj1, obj2) { 
     var i = 0, result = 0, numberOfProperties = props.length; 
     /* try getting a different result from 0 (equal) 
     * as long as we have extra properties to compare 
     */ 
     while(result === 0 && i < numberOfProperties) { 
      result = dynamicSort(props[i])(obj1, obj2); 
      i++; 
     } 
     return result; 
    } 
} 

我创建了一个数组如下:

var arr = [ 
    {a:"a",b:"a",c:"a"}, 
    {a:"b",b:"a",c:"b"}, 
    {a:"b",b:"a",c:"a"}, 
    {a:"b",b:"a",c:"b"}, 
    {a:"b",b:"b",c:"a"}, 
    {a:"b",b:"b",c:"b"}, 
    {a:"b",b:"b",c:"a"}, 
    {a:"b",b:"b",c:"b"}, 
    {a:"b",b:"b",c:"a"}, 
    {a:"b",b:"b",c:"b"}, 
    {a:"b",b:"b",c:"a"}, 
    {a:"c",b:"b",c:"b"}, 
    {a:"c",b:"c",c:"a"} 
]; 

,它工作时,我做到了,

arr.sort(dynamicSortMultiple("c","b","a")); 

这里是一个工作示例:http://jsfiddle.net/ZXedp/

+1

这一个呢,还具有多个属性:http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript/4760279#4760279 – 2013-05-10 08:44:34

1

你也可能想看看thenBy.js:https://github.com/Teun/thenBy.js

它允许您使用标准的Array.sort,但firstBy()thenBy()thenBy()的风格。

+0

更好: 'firstBy =(函数(){函数TB(Y){风险X =此;函数f(A,b){返回X(A,b)|| Y(A,b);} f.thenBy = TB;返回f;}返回函数(f){f.thenBy = tb;返回f;};})();' – Bergi 2013-08-04 17:46:30

+0

我感兴趣,为什么这会更好。你能详细说明吗?也许使用拉请求? – 2013-08-04 20:47:53

+0

1)只有一个,共享'thenBy'函数2)没有不必要的'secondaryFunction' 3)它更短4)它更实用,你可能会遇到'var a = firstBy(x),b = a.thenBy(y ),c = a.thenBy(z)'因为'a' =='b' =='c' – Bergi 2013-08-05 09:09:31

0

这是我的解决方案。它比lodash的_.sortBy()多列排序功能,更快的两倍左右(见http://jsperf.com/multi-column-sort。 我产生排序功能的文字,然后用它在标准的.sort(),它可以在Chrome和Firefox的为好。

function multiColumnSort(arr,sf) { 
    var s = ''; 
    sf.forEach(function(f,idx) { 
     s += 'if(arguments[0].'+f+'>arguments[1].'+f+')return 1;'; 
     s += 'else if(arguments[0].'+f+'==arguments[1].'+f+')'; 
     s += (idx < sf.length-1)? '{' : 'return 0'; 
    }); 
    s += Array(sf.length).join('}')+';return -1'; 
    return arr.sort(new Function(s)); 
}; 
5

最简单的方法来执行一个Javascript多准则排序(或者多参数排序),是使用.sort,多参数串联在一起,并比较两个蜇伤。

例如:

data.sort(function (a, b) { 

    var aConcat = a["property1"] + a["property2"]; 
    var bConcat = b["property1"] + b["property2"]; 

    if (aConcat > bConcat) { 
    return 1; 
    } else if (aConcat < bConcat) { 
    return -1; 
    } else { 
    return 0; 
    } 

}); 

我在这里已经包括了的jsfiddle脚本:http://jsfiddle.net/oahxg4u3/6/

1

我现在这个职位是很老了,反正我觉得今天引述爱琴奥兹坎,我改进他为任何感兴趣的人(http://jsfiddle.net/ZXedp/65/)实施DESC-ASC SQL-Like功能的出色解决方案:

function dynamicSortMultiple() { 
    var props=[]; 
    /*Let's separate property name from ascendant or descendant keyword*/ 
    for(var i=0; i < arguments.length; i++){ 
     var splittedArg=arguments[i].split(/ +/); 
     props[props.length]=[splittedArg[0], (splittedArg[1] ? splittedArg[1].toUpperCase() : "ASC")]; 
    } 
    return function (obj1, obj2) { 
     var i = 0, result = 0, numberOfProperties = props.length ; 
     /*Cycle on values until find a difference!*/ 
     while(result === 0 && i < numberOfProperties) { 
      result = dynamicSort(props[i][0], props[i][1])(obj1, obj2); 
      i++; 
     } 
     return result; 
    } 
} 

/*Base function returning -1,1,0 for custom sorting*/ 
function dynamicSort(property, isAscDesc) { 
    return function (obj1,obj2) { 
     if(isAscDesc==="DESC"){ 
      return ((obj1[property] > obj2[property]) ? (-1) : ((obj1[property] < obj2[property]) ? (1) : (0))); 
     } 
     /*else, if isAscDesc==="ASC"*/ 
     return ((obj1[property] > obj2[property]) ? (1) : ((obj1[property] < obj2[property]) ? (-1) : (0))); 
    } 
} 

呼叫功能通过这样的事:

arr.sort(dynamicSortMultiple("c DESC","b Asc","a"));