Skip to content
Advertisement

Ordering an array of objects in javascript [closed]

I have this array:

enter image description here

It has 16 positions and each position is an object with an ID and a content. I have to order this array alphabetically by it’s content. I tried to use sort, but it didn’t work. Can someone help me with this ?

Advertisement

Answer

You should implement the sort method like that:

elems.sort(function(a, b) {
    return a.content - b.content;
});

Or using ES6:

elems.sort((a, b) => a.content - b.content);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement