0

I want to sort an objects array by id and name.

Sample data:

 [
 {
      "case": {
        "name": "Exemple1"
      },
      "id": 3
    },
    {
      "case": {
        "name": "Exemple1"
      },
      "id": 2
    },
    {
      "case": {
        "name": "Exemple2"
      },
      "id": 1
    },
    {
      "case": {
        "name": "Exemple3"
      },
      "id": 4
    },
    {
      "case": {
        "name": "Exemple2"
      },
      "id": 6
    }
  ]

I want to sort by id and the name field in the case object ? Is it possible to use two criteria in sort?

Expected result:

 [
    {
      "case": {
        "name": "Exemple1"
      },
      "id": 2
    },
        {
      "case": {
        "name": "Exemple1"
      },
      "id": 3
    },
    {
      "case": {
        "name": "Exemple2"
      },
      "id": 1
    },
    {
      "case": {
        "name": "Exemple2"
      },
      "id": 6
    },
    {
      "case": {
        "name": "Exemple3"
      },
      "id": 4
    }
 ]

How can I use two criteria in my sort?

D M
  • 5,769
  • 4
  • 12
  • 27
Heptagram
  • 105
  • 1
  • 9
  • 1
    Does this answer your question? [Javascript - sorting array by multiple criteria](https://stackoverflow.com/questions/28560801/javascript-sorting-array-by-multiple-criteria), [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), [Javascript array sorting based on multiple criteria (in one iteration)](https://stackoverflow.com/questions/61857654/javascript-array-sorting-based-on-multiple-criteria-in-one-iteration) – D M Nov 01 '21 at 16:40

2 Answers2

4

Sure it's possible.

The Array#sort method takes an optional parameter called compareFunction that represents the logic to perform when sorting any two items in the array.

By passing a function that compares first by case.name then by id, you can sort your array by both criteria. Here's a simple example, though you'll likely want to add error checking so you don't try reading an undefined property.

const unsortedData =  [
  { "case": { "name": "Exemple1" }, "id": 3 },
  { "case": { "name": "Exemple1" }, "id": 2 },
  { "case": { "name": "Exemple2" }, "id": 1 },
  { "case": { "name": "Exemple3" }, "id": 4 },
  { "case": { "name": "Exemple2" }, "id": 6 }
];

const sortedData = unsortedData.sort((a, b) => {
  if (a.case.name > b.case.name) return 1;
  if (a.case.name < b.case.name) return -1;
  if (a.id > b.id) return 1;
  if (a.id < b.id) return -1;
  return 0;
});

console.dir(sortedData);
D M
  • 5,769
  • 4
  • 12
  • 27
  • @D M, thank you for your help, It's work and i can use this exemple for a better understanding, and use for other sort. – Heptagram Nov 01 '21 at 17:19
1

You can use array#sort with custom comparator. You can use conditional OR to compare on multiple condition.

arr.sort((a,b) => a.case.name.localeCompare(b.case.name) || a.id - b.id)

const arr = [{ "case": { "name": "Exemple1" }, "id": 3 }, { "case": { "name": "Exemple1" }, "id": 2 }, { "case": { "name": "Exemple2" }, "id": 1 }, { "case": { "name": "Exemple3" }, "id": 4 }, { "case": { "name": "Exemple2" }, "id": 6 } ];
arr.sort((a,b) => a.case.name.localeCompare(b.case.name) || a.id - b.id);
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51