JavaScript中自定义对象的排序数组

JavaScript中自定义对象的排序数组,javascript,object,Javascript,Object,假设我有一个Employee对象数组: var Employee = function(fname, age) { this.fname = fname; this.age = age; } var employees = [ new Employee("Jack", "32"), new Employee("Dave", "31"), new Employee("Rick", "35"), new Employee("Anna", "33")

假设我有一个Employee对象数组:

var Employee = function(fname, age) {
    this.fname = fname;
    this.age = age;
}

var employees = [
    new Employee("Jack", "32"),
    new Employee("Dave", "31"),
    new Employee("Rick", "35"),
    new Employee("Anna", "33")
];

此时,
employees.sort()
没有任何意义,因为解释器不知道如何对这些自定义对象进行排序。所以我传入了我的自定义排序函数

employees.sort(function(employee1, employee2){
    return employee1.age > employee2.age;
});

现在
employees.sort()
非常有效。

但如果我也想控制要排序的字段,在运行时以某种方式将其传入,该怎么办?我可以这样做吗

employees.sort(function(employee1, employee2, on){
    if(on === 'age') {
        return employee1.age > employee2.age;
    }
    return employee1.fname > employee2.fname;
});
我不能让它工作,所以建议?也许是基于设计模式的重构

function getSortFunction(fieldName) {
    return function(employee1, employee2) {
        return employee1[fieldName] > employee2[fieldName];
    }
}

employees.sort(getSortFunction("myField"));
另一个解决方案是,如果你不怕,就使用:)


你可以使用优秀图书馆的方法

例如:

var arr = [
    { name:"a", age:100 },  
    { name:"b", age:90 },
    { name:"c", age:80 },
    { name:"d", age:70 }
];

var sorted = _.sortBy(arr, "age");
console.log( sorted );
或者在您的情况下:

_.sortBy(employees, "age");
我提供的数组允许您使用以下命令指定任意数量的字段进行排序:

例1 例2: 代码
(函数(){
//此代码由Gavin Kistner于2012年版权所有,!@phrogz.net
//许可证:http://phrogz.net/JS/_ReuseLicense.txt
if(typeof Object.defineProperty==='function'){
试试{Object.defineProperty(Array.prototype,'sortBy',{value:sb});}catch(e){}
}
如果(!Array.prototype.sortBy)Array.prototype.sortBy=sb;
函数sb(f){
for(var i=此.length;i;){
var o=这个[--i];
这个[i]=[].concat(f.call(o,o,i),o);
}
this.sort(函数(a,b){

对于(var i=0,len=a.length;iYou是受欢迎的。下划线还有一个替代选项。在SO的精神中,这真的值得版权保护吗?我发现了类似于此的东西=>objSort.js(),但你的更简单,更直截了当。+1
_.sortBy(employees, "age");
var a=[ {c:"GK",age:37}, {c:"ZK",age:13}, {c:"TK",age:14}, {c:"AK",age:13} ];

a.sortBy( function(){ return this.age } );                                   
// [ {c:"ZK",age:13}, {c:"AK",age:13}, {c:"TK",age:14}, {c:"GK",age:37} ] 

a.sortBy( function(){ return [this.age,this.c] } );                          
// [ {c:"AK",age:13}, {c:"ZK",age:13}, {c:"TK",age:14}, {c:"GK",age:37} ] 

a.sortBy( function(){ return -this.age } );                                  
// [ {c:"GK",age:37}, {c:"TK",age:14}, {c:"ZK",age:13}, {c:"AK",age:13} ] 
var n=[ 1, 99, 15, "2", "100", 3, 34, "foo", "bar" ];                        

n.sort();                                                                    
// [ 1, "100", 15, "2", 3, 34, 99, "bar", "foo" ]                         

n.sortBy( function(){ return this*1 } );                                     
// [ "foo", "bar", 1, "2", 3, 15, 34, 99, "100" ]                         

n.sortBy( function(o){ return [typeof o,this] } );                           
// [1, 3, 15, 34, 99, "100", "2", "bar", "foo"]                           

n.sortBy(function(o){ return [typeof o, typeof o=="string" ? o.length : o] })
// [1, 3, 15, 34, 99, "2", "100", "bar", "foo"]                           
(function(){
  // This code is copyright 2012 by Gavin Kistner, !@phrogz.net
  // License: http://phrogz.net/JS/_ReuseLicense.txt
  if (typeof Object.defineProperty === 'function'){
    try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
  }
  if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;

  function sb(f){
    for (var i=this.length;i;){
      var o = this[--i];
      this[i] = [].concat(f.call(o,o,i),o);
    }
    this.sort(function(a,b){
      for (var i=0,len=a.length;i<len;++i){
        if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
      }
      return 0;
    });
    for (var i=this.length;i;){
      this[--i]=this[i][this[i].length-1];
    }
    return this;
  }
})();