I got an array (see below for one object in the array) that I need to sort by firstname using JavaScript.
How can I do it?

var user = {
   bio: null,
   email:  "user@domain.com",
   firstname: "Anna",
   id: 318,
   lastAvatar: null,
   lastMessage: null,
   lastname: "Nickson",
   nickname: "anny"
};

23 s
23

Shortest possible code with ES6!

users.sort((a, b) => a.firstname.localeCompare(b.firstname))

String.prototype.localeCompare() basic support is universal!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *