Ordering an array of objects in javascript [closed]

Tags:



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 ?

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);


Source: stackoverflow