在React JS中按日期排序数组

时间:2016-12-09 10:49:00

标签: javascript reactjs

我有一个对象数组如下:

let cars = [
    {"id":20,"mileage":41300,"make":"Golf", initialRegistration:"09/02/2010"}, 
    {"id":21,"mileage":51300,"make":"Passat", initialRegistration:"06/04/2012"},
    {"id":22,"mileage":61300,"make":"Audi", initialRegistration:"02/01/2018"},
    {"id":23,"mileage":20300,"make":"Touran", initialRegistration:"17/09/2013"},
    {"id":24,"mileage":10300,"make":"Polo", initialRegistration:"26/07/2014"}
];

我希望按initialRegistration排序。

我试着这样做:

let sortedCars = cars.sort((a, b) => Date.parse(a.initialRegistration) - Date.parse(b.initialRegistration));

let sortedCars1 = cars.sort((a, b) => new Date(a.initialRegistration) - new Date(b.initialRegistration));

let sortedCars2 = cars.sort((a, b) => new Date(a.initialRegistration).getTime() - new Date(b.initialRegistration).getTime());

但这些尝试都没有奏效。我得到的结果是:

Golf - 09/02/2010
Passat - 06/04/2012
Audi - 02/01/2018
Touran - 17/09/2013
Polo - 26/07/2014

Here is fiddle.

3 个答案:

答案 0 :(得分:6)

使用Date构造函数并执行类似的操作。

let sortedCars1 = cars.sort((a, b) => new Date(...a.initialRegistration.split('/').reverse()) - new Date(...b.initialRegistration.split('/').reverse()));

let cars = [{
  "id": 20,
  "mileage": 41300,
  "make": "Golf",
  initialRegistration: "09/02/2010"
}, {
  "id": 21,
  "mileage": 51300,
  "make": "Passat",
  initialRegistration: "06/04/2012"
}, {
  "id": 22,
  "mileage": 61300,
  "make": "Audi",
  initialRegistration: "02/01/2018"
}, {
  "id": 23,
  "mileage": 20300,
  "make": "Touran",
  initialRegistration: "17/09/2013"
}, {
  "id": 24,
  "mileage": 10300,
  "make": "Polo",
  initialRegistration: "26/07/2014"
}];

let sortedCars1 = cars.sort((a, b) => new Date(...a.initialRegistration.split('/').reverse()) - new Date(...b.initialRegistration.split('/').reverse()));

console.log(sortedCars1);

或者在撤消日期字符串后使用String#localeCompare进行字符串比较。

let sortedCars1 = cars.sort((a, b) =>
  a.initialRegistration.split('/').reverse().join().localeCompare(b.initialRegistration.split('/').reverse().join())); 

let cars = [{
  "id": 20,
  "mileage": 41300,
  "make": "Golf",
  initialRegistration: "09/02/2010"
}, {
  "id": 21,
  "mileage": 51300,
  "make": "Passat",
  initialRegistration: "06/04/2012"
}, {
  "id": 22,
  "mileage": 61300,
  "make": "Audi",
  initialRegistration: "02/01/2018"
}, {
  "id": 23,
  "mileage": 20300,
  "make": "Touran",
  initialRegistration: "17/09/2013"
}, {
  "id": 24,
  "mileage": 10300,
  "make": "Polo",
  initialRegistration: "26/07/2014"
}];

let sortedCars1 = cars.sort((a, b) =>
  a.initialRegistration.split('/').reverse().join().localeCompare(b.initialRegistration.split('/').reverse().join()));

console.log(sortedCars1);

答案 1 :(得分:3)

实际上日期解析没有正确发生新日期(“09/02/2010”)不起作用,并假设日期为2010年第2季度因此需要传递为“2010-02-09”yyyy- MM-DD

  let sortedCars = cars.sort((a, b) => Date.parse(new Date(a.initialRegistration.split("/").reverse().join("-"))) - Date.parse(new Date(b.initialRegistration.split("/").reverse().join("-"))));

请参阅此http://jsfiddle.net/jwm6k66c/1590/

答案 2 :(得分:0)

你的排序方式是正确的,只需更改cars数组中的日期格式,使其成为MM:DD:YYYY或YYYY:MM:DD它将起作用,因为这些是Date解析接受的格式。