按属性值对JavaScript对象数组进行排序[duplicate]

时间:2021-12-19 16:02:02

This question already has an answer here:

这个问题已经有了答案:

I have an array of JavaScript objects. My array is defined like this:

我有一个JavaScript对象数组。我的数组定义如下:

var myObjects = [
  { id: '1', username: 'bill.jones', active: true, createdon: '03/29/2014' },
  { id: '2', username: 'woohoo', active: true, createdon: '03/28/2014' },
  { id: '3', username: 'someuser', active: true, createdon: '03/30/2014' }
];

This array is actually dynamically populated. Still, I need to sort the results by the createdon value in ascending order. To do that, I'm trying to use lodash. The createdon value represents a date. Currently, I'm trying the following:

这个数组实际上是动态填充的。尽管如此,我仍然需要按createdon值按升序对结果进行排序。为了做到这一点,我尝试使用lodash。createdon值表示一个日期。目前,我正在尝试以下方法:

//  ORDER BY createdOn
myObjects.sort(function (a, b) {
  var date1 = new Date(a['createdon']);
  var date2 = new Date(b['createdon']);
  return date1 < date2;
});

_.forEach(myObjects, function(result) {
  console.log(result);
});

Unfortunately, myObjects is still not sorted after I run this function. What am I doing wrong?

不幸的是,在运行这个函数之后,myobject仍然没有排序。我做错了什么?

Thank you!

谢谢你!

2 个解决方案

#1


62  

I just went through lodash doc, and perhaps you could try sortBy

我刚刚看过《疯狂医生》,也许你可以试试《sortBy》

Try it: http://jsfiddle.net/3Wza8/

试一试:http://jsfiddle.net/3Wza8/

var myObjects = [
    { id: '1', username: 'bill.jones', active: true, createdon: new Date('03/29/2014') },
    { id: '2', username: 'woohoo', active: true, createdon: new Date('03/28/2014') },
    { id: '3', username: 'someuser', active: true, createdon: new Date('03/30/2014') }
];

myObjects = _.sortBy(myObjects, 'createdon');

_.forEach(myObjects, function (result) {
    console.log(result);
});

EDIT: as Cookie Monster pointed out, it's important that your createdon field is a Date, and not a string.

编辑:正如Cookie Monster指出的那样,createdon字段是一个日期,而不是字符串,这一点很重要。

#2


0  

The problem is that sort expects a function which returns -1, 0 or 1. Your function only returns 0 and 1.

问题是排序要求返回-1、0或1的函数。函数只返回0和1。

This slight change should fix it:

这个小小的改变应该可以修复它:

myObjects.sort(function (a, b) {
  var date1 = new Date(a['createdon']);
  var date2 = new Date(b['createdon']);
  return date1 - date2;
});

#1


62  

I just went through lodash doc, and perhaps you could try sortBy

我刚刚看过《疯狂医生》,也许你可以试试《sortBy》

Try it: http://jsfiddle.net/3Wza8/

试一试:http://jsfiddle.net/3Wza8/

var myObjects = [
    { id: '1', username: 'bill.jones', active: true, createdon: new Date('03/29/2014') },
    { id: '2', username: 'woohoo', active: true, createdon: new Date('03/28/2014') },
    { id: '3', username: 'someuser', active: true, createdon: new Date('03/30/2014') }
];

myObjects = _.sortBy(myObjects, 'createdon');

_.forEach(myObjects, function (result) {
    console.log(result);
});

EDIT: as Cookie Monster pointed out, it's important that your createdon field is a Date, and not a string.

编辑:正如Cookie Monster指出的那样,createdon字段是一个日期,而不是字符串,这一点很重要。

#2


0  

The problem is that sort expects a function which returns -1, 0 or 1. Your function only returns 0 and 1.

问题是排序要求返回-1、0或1的函数。函数只返回0和1。

This slight change should fix it:

这个小小的改变应该可以修复它:

myObjects.sort(function (a, b) {
  var date1 = new Date(a['createdon']);
  var date2 = new Date(b['createdon']);
  return date1 - date2;
});