Sort array alphabetic by name


#1

Hello.

I have an array in type of:
itemList = [{
name: ‘One’,
category: ‘cat’,
id: ‘5’
},
{
name: ‘Apple’,
category: ‘cat’,
id: ‘6’
}];

And i’m trying to sort the items alphabetic by the name. I use ngFor to display them all inside the html template. Is there a way to sort the array alphabetic in the typescript or html file? I tried some things that I found but most of them where outdated and didn’t work anymore.

Thanks in advance for the help!

Edit 1: Forget the brackets.


#2

As you’ve written it, that’s not an array. Arrays are bounded by []. That being said, the MDN page on Array.prototype.sort contains an example very close to your situation.


#3

If your data source is a json file, use map() then sort(). Easy.


#4

I forget the brackets. Stupid of me. I will see of I can get to understand the page you gave me. Thanks :slight_smile:


#5

You’re not stupid, everyone need help sometime or forget the thing, the community is here to help.