0

I have this array of objects, (of course it's actually much longer), and I would like to sort it based on the uid properties by first grouping all the uids that have the X, and then the ones that only have the "number" (or on the contrary, it is the same, but the important thing is that they are grouped => all "X" and all "NOT X". It does not matter that they are also sorted).

 [ 
  { id: 114, iid: {uid: "12", xe: 9 }, foo: 123, },
  { id: 120, xid: {uid: "X-12", xe: 9 }, foo: 123, },
  { id: 133, iid: {uid: "31", xe: 9 }, foo: 123, },
  { id: 120, xid: {uid: "X-41", xe: 9 }, foo: 123, },
  //And many others....
 ]

So it should become something like:

 [ 
  { id: 120, xid: {uid: "X-41", xe: 9 }, foo: 123, }
  { id: 120, xid: {uid: "X-12", xe: 9 }, foo: 123, },
  { id: 133, iid: {uid: "31", xe: 9 }, foo: 123, },
  { id: 114, iid: {uid: "12", xe: 9 }, foo: 123, },
  //And many others....
 ]
Luca
  • 335
  • 3
  • 19
  • 1
    question was closed before i could post: `function sorter(a, b) { const {uid} = a?.xid ?? a?.iid; return uid.startsWith("X") ? -1 : 0 }` use it as `data.sort(sorter)` – Mulan Nov 09 '22 at 17:24
  • Yes, unfortunately, it brings me back to an answer that is not for me, but obviously those who have closed it or have not read it or understand very little about the code. Thanks for your answer, I would have accepted it as the correct answer. – Luca Nov 09 '22 at 17:53

0 Answers0