0

I'm not very skilled with JavaScript and I have an issue where I need to sort array based on description. I looked several topics and I have tried to sort it but so far no luck..

I have an array of objects that looks like this:

{
  category: null,
  code: "GGG",
  id: 3,
  description: "Gama"
},    
{
  category: null,
  code: "AAA",
  id: 1,
  description: "Alfa"
},
{
  category: null,
  code: "BBB",
  id: 2,
  description: "Beta"
}

And this array is returned by a function that says:

return result.data

So want to sort that result set based on description on ascending order, which means my list would look like this after the return: "Alfa", "Beta", "Gama".

I was trying to use sort function:

return result.data.sort();

But that didn't help me.

JustQuest
  • 249
  • 4
  • 15

1 Answers1

1

You can do:

result.data.sort((a, b) => a.description.localeCompare(b.description)));
lante
  • 7,192
  • 4
  • 37
  • 57