0

I am trying to sort some reservations and want them to be sorted by ascending order (The reservations with less days past to show first) but I don't think I am using sorting correctly. can you please help me? Example1Example2

{
  "customerId": "oliver.test@test.com",
  "dueDate": 1614409200000,
  "reservationItems": [
    {
      "description": "Paddle",
      "itemId": 4949490,
      "returned": false
    },
    {
      "description": "Boat",
      "itemId": 4949490,
      "returned": false
    },
    {
      "description": "Boat",
      "itemId": 4949490,
      "returned": false
    },
    {
      "description": "Snowboard leash",
      "itemId": 4949494,
      "returned": false
    }
  ]
}

.filter((customer) =>
  customer.reservationItems.some((item) => !item.returned)
).sort((item) => item.dueDate);
EdwardLi
  • 196
  • 1
  • 9

2 Answers2

0

Considering that the API response is in an array format (you have only posted the object example but wanted to achieve the sorting using array filter method). Please try the below code to sort based on dueDate.

API response in an array format

var arr = [
  {
    customerId: "oliver.test@test.com",
    dueDate: 1614409200000,
    reservationItems: [
      { description: "Paddle", itemId: 4949490, returned: false },
      { description: "Boat", itemId: 4949490, returned: false },
      { description: "Boat", itemId: 4949490, returned: false },
      { description: "Snowboard leash", itemId: 4949494, returned: false },
    ],
  },
  {
    customerId: "oliver1.test@test.com",
    dueDate: 161440920000,
    reservationItems: [
      { description: "Paddle", itemId: 4949490, returned: false },
      { description: "Boat", itemId: 4949490, returned: false },
      { description: "Boat", itemId: 4949490, returned: false },
      { description: "Snowboard leash", itemId: 4949494, returned: false },
    ],
  },
];

Sorting:

let sortedList = arr
  .filter((customer) =>
    customer.reservationItems.some((item) => !item.returned)
  )
  .sort((a, b) => a.dueDate - b.dueDate);

console.log(sortedList);

Check the output here -> https://codepen.io/askannan/pen/PobRbZq

Edit:

The javascript sort() method will produce an incorrect result when sorting numbers. We can fix this by providing a "compare function". (return a-b)

let arrSort = [11,2,6,5].sort() -> This will return -> [11,2,5,6]

Because it compared only the first character so the javascript considered 11 as a smaller number. (try this in chrome console)

EdwardLi
  • 196
  • 1
  • 9
Kannan
  • 184
  • 9
  • now I am trying to add a conditional in the sorting. I want to display ascending order if the numbers are 0 to negative numbers and descending if the numbers are greater than 0 but doesnt seem to work by any change do you have any idea? `.filter(customer => customer.reservationItems.some(item => !item.returned)).sort((a,b) => { if (a,b < 0){ return b.dueDate - a.dueDate }else{ return b.dueDate + a.dueDate }})` – Eduardo Urbiola Mar 01 '21 at 01:32
  • Hi, what are the numbers here? is that duedate or any specific property in the object? – Kannan Mar 01 '21 at 12:47
0

Ascending:

.sort((a, b) => new Date(a.dueDate) - new Date(b.dueDate))

Descending:

.sort((a, b) => new Date(b.dueDate) - new Date(a.dueDate))
Emmanuel
  • 4,364
  • 5
  • 38
  • 63