Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array by dates in React JS

I have an array of objects as follows :

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"}
];

And I want to sort it by initialRegistration.

I tried to do it as follows :

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());

But none of these attempts did not work. The results that I get is :

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

Here is fiddle.

like image 709
Boky Avatar asked Dec 09 '16 10:12

Boky


People also ask

How do you sort an array by date?

To sort an array of objects by date property: Call the sort() method on the array. Subtract the date in the second object from the date in the first. Return the result.

How do you sort data on the basis of date in react JS?

Your sorting way is proper, just change the date format in cars array, make it MM:DD:YYYY, or YYYY:MM:DD it will work, because these are the formats that Date parse accept.

How do I sort a date in TypeScript?

To sort an array of objects by date in TypeScript: Call the sort() method on the array, passing it a function. The function will be called with 2 objects from the array. Subtract the timestamp of the date in the second object from the timestamp of the date in the first.


2 Answers

Use Date constructor and do something like this.

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);

Or by string comparison using String#localeCompare after reversing the date string.

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);
like image 161
Pranav C Balan Avatar answered Sep 30 '22 05:09

Pranav C Balan


Actually the date parsing was not happening correctly new Date("09/02/2010") will not work and will assume date to be 2nd sep 2010 hence it need sto be passed as "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("-"))));

See this http://jsfiddle.net/jwm6k66c/1590/

like image 43
Vinod Louis Avatar answered Sep 30 '22 07:09

Vinod Louis