按日期排序JavaScript对象数组 [英] Sort Javascript Object Array By Date

查看:122
本文介绍了按日期排序JavaScript对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有几个对象的数组:

  var array = [{id:1,date:Mar 12 2012 10:00:00 AM},{id:2,date:Mar 8 2012 08:00:00 AM}]; 

如何按日期元素排序这个数组,从最接近当前日期的日期开始,时间下降?请记住,数组可能有很多对象,但是为了简单起见,我使用了2。



我会使用排序函数和自定义比较器吗?



更新



在我的具体情况下,我希望从最近到最老的。最后,我不得不反转简单函数的逻辑:

  array.sort(function(a,b){ 
a = new Date(a.dateModified);
b = new Date(b.dateModified);
return a> b?-1:a< b?1:0;
});

这是从最近的日期排序。

解决方案

最简单的答案



  array.sort(function(a,b){ 
//将您的字符串转换为日期,然后减去
//以获取一个负值,正数或零值。
返回新日期(b.date) - 新日期(a.date);
});



更多通用答案



  array.sort(function(o1,o2){
if(sort_o1_before_o2)return -1;
else if(sort_o1_after_o2)return 1;
else return 0;
});

或更简洁:

  array.sort(function(o1,o2){
return sort_o1_before_o2?-1:sort_o1_after_o2?1:0;
});



通用,强大的答案



定义一个在所有数组上使用 Schwartzian变换的自定义非枚举 sortBy 函数:$ {

 (function(){
if(typeof Object.defineProperty ==='function'){
try {Object.defineProperty(Array.prototype,sortBy',{value:sb});} catch(e){}
}
if(!Array.prototype.sortBy)Array。 prototype.sortBy = sb;

函数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 if(a [i]!= b [i])return a [i]< b [i]? - 1:1;
}
return 0;
});
(var i = this.length; i;){
this [--i] = this [i] [this [i] .length-1];
}
return this;
}
})();

使用它像这样:

  array.sortBy(function(o){return o.date}); 

如果您的日期不能直接比较,请将其作为可比日期,例如

  array.sortBy(function(o){return new Date(o.date)}); 

如果返回值的数组,您还可以使用此方法对多个条件进行排序:

  //按日期排序,然后得分(反转),然后命名
array.sortBy(function(o){return [ o.date,-o.score,o.name]};

请参阅 http://phrogz.net/JS/Array.prototype.sortBy.js 了解更多详情。


Say I have an array of a few objects:

var array = [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 08:00:00 AM}];

How can I sort this array by the date element in order from the date closest to the current date and time down? Keep in mind that the array may have many objects, but for the sake of simplicity I used 2.

Would I use the sort function and a custom comparator?

UPDATE

In my specific case, I wanted the dates arranged from the most recent to the oldest. It ended up that I had to reverse the simple function's logic as so:

array.sort(function(a, b) {
    a = new Date(a.dateModified);
    b = new Date(b.dateModified);
    return a>b ? -1 : a<b ? 1 : 0;
});

This sorts the dates from the most recent.

解决方案

Simplest Answer

array.sort(function(a,b){
  // Turn your strings into dates, and then subtract them
  // to get a value that is either negative, positive, or zero.
  return new Date(b.date) - new Date(a.date);
});

More Generic Answer

array.sort(function(o1,o2){
  if (sort_o1_before_o2)    return -1;
  else if(sort_o1_after_o2) return  1;
  else                      return  0;
});

Or more tersely:

array.sort(function(o1,o2){
  return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});

Generic, Powerful Answer

Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :

(function(){
  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;
  }
})();

Use it like so:

array.sortBy(function(o){ return o.date });

If your date is not directly comparable, make a comparable date out of it, e.g.

array.sortBy(function(o){ return new Date( o.date ) });

You can also use this to sort by multiple criteria if you return an array of values:

// Sort by date, then score (reversed), then name
array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };

See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.

这篇关于按日期排序JavaScript对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆