-2

I have this array:

array: [
    {id: 1, title: 'Thanos', content: '123'},
    {id: 2, title: 'Deadpool', content: '456'},
    {id: 3, title: 'Batman', content: '789'}
]

and I want it to be sorted by title key. How do I do that in Javascript?

Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132
  • 2
    That's not a 2D array. It's an array of objects. – jabaa Mar 13 '22 at 12:52
  • see: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – pilchard Mar 13 '22 at 13:17
  • I know it's difficult to understand if you don't know, that the original question was a different one and @jabaa pointed out that I was in fact looking for a different object. So I changed it. Thank you for your concern though. – Artur Müller Romanov Mar 13 '22 at 13:19

1 Answers1

1
array.sort((a, b) => a.title.localeCompare(b.title));
martincarlin87
  • 10,848
  • 24
  • 98
  • 145