0
Q:

js order alphabetically

By default, the sort() method sorts the values as strings in alphabetical and ascending order.
This works well for strings ("Apple" comes before "Banana"). However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce an incorrect result when sorting numbers.
You can fix this by providing a "compare function"

numbers = [2, 3, 41, 3, 2, 4, 5];

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

// For more info on the compare function see https://www.w3schools.com/jsref/jsref_sort.asp
11
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
2
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
2
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
1
// Alphabetically
const ascending = data.sort((a, b) => a[field].localeCompare(b[field]))
// Descending
const descending = ascending.reverse()
1

New to Communities?

Join the community