Bonjour,

J'essaie de trier un tableau d'objets par id (id mongoDb de type string) :

Le tableau :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const books = [
  {
    "_id": "62a4c7eff50ca49efc9e2c4c",
    "title": "Titre 1",
    "description": "Lorem Mirage",
    "author": "Fill Post",
    "createdAt": "2022-06-11T16:50:55.231Z",
    "updatedAt": "2022-06-15T20:21:32.671Z",
    "__v": 0
  },
  {
    "_id": "62a4c86cf50ca49efc9e2c54",
    "title": "Titre 2",
    "description": "Lorem",
    "author": "Jack Laurence",
    "createdAt": "2022-06-11T16:53:00.838Z",
    "updatedAt": "2022-06-15T20:17:42.382Z",
    "__v": 0
  },
  {
    "_id": "62a4c8de567beddd72fb5c1c",
    "title": "Titre 3",
    "description": "Lorem",
    "author": "Loua guo",
    "createdAt": "2022-06-11T16:54:54.945Z",
    "updatedAt": "2022-06-11T16:54:54.945Z",
    "__v": 0
  }
 
]
Ma tentative de tri avec la fonction sort() :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
const booksE = books.slice().sort((a, b) => b._id - a._id);
console.log(booksE); // output :  ordre du tableau books inchangé donc tri inopérant
Le tri ne fonctionne pas. Une idée pour arriver à mes fins ? Merci.