sort javascript string code example

Example 1: array sort by alphabetical javascript

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

Example 2: javascript order by string array

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

Example 3: javascript sort

homes.sort(function(a, b) {
    return parseFloat(a.price) - parseFloat(b.price);
});

Example 4: javascript sort

homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));

Example 5: string sort javascript

var names = ["Peter", "Emma", "Jack", "Mia"];
var sorted = names.sort(); // ["Emma", "Jack", "Mia", "Peter"]

Tags: