Sort array by dates in React JS

前端 未结 3 1722
遥遥无期
遥遥无期 2020-12-31 23:24

I have an array of objects as follows :

let cars = [
    {\"id\":20,\"mileage\":41300,\"make\":\"Golf\", initialRegistration:\"09/02/2010\"}, 
    {\"id\":21         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-01 00:03

    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/

提交回复
热议问题