0

I want it to be sorted by date and alphabet in one sort how can i do that ? I think alphabetical order works good but date not works properly. Thanks for answers.

Data structure :

[{
    productId: 21,
    title: "Huawei P40 Lite ",
    brand: "Huawei",
    price: 120,
    discountPercentage: 10,
    color: "Black",
    createdDate: "2021-01-15T01:00:00+03:00",
  },
  {
    productId: 22,
    title: "Huawei P40 Lite",
    brand: "Huawei",
    price: 1026,
    discountPercentage: 0,
    color: "Green",
    createdDate: "2021-01-16T01:00:00+03:00",
  },
  {
    productId: 23,
    title: "Apple iPhone 11",
    brand: "Apple",
    price: 1220,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-17T01:00:00+03:00",
  },
 {
    productId: 24,
    title: "Apple iPhone 12",
    brand: "Apple",
    price: 1420,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-18T01:00:00+03:00",
  }],

Here my work :

    jsfiddle.net/pazyqb01/

And tried different solutions for sort date somehow i couldn't make it work.

Sorted Array shoul be like above :

 {
    productId: 24,
    title: "Apple iPhone 12",
    brand: "Apple",
    price: 1420,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-18T01:00:00+03:00",
  },
{
    productId: 23,
    title: "Apple iPhone 11",
    brand: "Apple",
    price: 1220,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-17T01:00:00+03:00",
  },
 {
    productId: 22,
    title: "Huawei P40 Lite",
    brand: "Huawei",
    price: 1026,
    discountPercentage: 0,
    color: "Green",
    createdDate: "2021-01-16T01:00:00+03:00",
  },
{
    productId: 21,
    title: "Huawei P40 Lite ",
    brand: "Huawei",
    price: 120,
    discountPercentage: 10,
    color: "Black",
    createdDate: "2021-01-15T01:00:00+03:00",
  },
Deniz Firat
  • 159
  • 1
  • 1
  • 10
  • 1
    Don't link to jsfiddle. You can create a code snippet right in your question that does the same thing. [edit] and click the `<>` button. [I've been told to create a "runnable" example with "Stack Snippets", how do I do that?](https://meta.stackoverflow.com/a/358993) – 001 Oct 03 '21 at 13:50
  • 1
    Does this answer your question? [How to sort an array of objects by multiple fields?](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) and [Sort array by ISO 8601 date](https://stackoverflow.com/questions/12192491/sort-array-by-iso-8601-date) – pilchard Oct 03 '21 at 13:55

1 Answers1

0

this way:

simply follow the list of your sorting criteria

const data = 
  [ { productId: 21, title: 'Huawei P40 Lite ', brand: 'Huawei', price:  120, discountPercentage: 10, color: 'Black', createdDate: '2021-01-15T01:00:00+03:00' } 
  , { productId: 22, title: 'Huawei P40 Lite',  brand: 'Huawei', price: 1026, discountPercentage: 0,  color: 'Green', createdDate: '2021-01-16T01:00:00+03:00' } 
  , { productId: 23, title: 'Apple iPhone 11',  brand: 'Apple',  price: 1220, discountPercentage: 11, color: 'White', createdDate: '2021-01-17T01:00:00+03:00' } 
  , { productId: 24, title: 'Apple iPhone 12',  brand: 'Apple',  price: 1420, discountPercentage: 11, color: 'White', createdDate: '2021-01-18T01:00:00+03:00' } 
  ] 

const fSort = (a,b) =>
  {
  let Dx = new Date(b.createdDate) - new Date(a.createdDate)     // 1st criteria
  if (Dx===0) Dx = a.title.trim().localeCompare(b.title.trim()) // 2nd

  // if (Dx===0) Dx = ... // 3rd
  // if (Dx===0) Dx = ... // 4th....
  return Dx
  }

console.log( data.sort(fSort))
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • 1
    Just flag it as [duplicate](https://stackoverflow.com/a/46256174/13762301). And you can replace the `if(...)` with an `||` – pilchard Oct 03 '21 at 14:14