Shortest possible code with ES6!
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
String.prototype.localeCompare() basic support is universal!
Answer from Nachiketha on Stack OverflowShortest possible code with ES6!
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
String.prototype.localeCompare() basic support is universal!
Suppose you have an array users. You may use users.sort and pass a function that takes two arguments and compare them (comparator)
It should return
- something negative if first argument is less than second (should be placed before the second in resulting array)
- something positive if first argument is greater (should be placed after second one)
- 0 if those two elements are equal.
In our case if two elements are a and b we want to compare a.firstname and b.firstname
Example:
users.sort(function(a, b){
if(a.firstname < b.firstname) { return -1; }
if(a.firstname > b.firstname) { return 1; }
return 0;
})
This code is going to work with any type.
Note that in "real life"™ you often want to ignore case, correctly sort diacritics, weird symbols like ß, etc. when you compare strings, so you may want to use localeCompare. See other answers for clarity.
How can I sort a Javascript array alphabetically? - Stack Overflow
Sort an Array Alphabetically using the sort Method
Sort array alphabetically javascript - Stack Overflow
javascript - Sort objects in an array alphabetically on one property of the array - Stack Overflow
Your array already contains strings of names where the last name is the first part of the string, so you can just call the .sort() method.
Oh, and your array contains a typo:
'Blake, 'William' should be: 'Blake, William'
const people = [ 'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig', 'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter' ];
console.log(people.sort());
The variables are simply mislabelled, see the following:
const alpha = people.sort((a, b) => {
const [aLast, aFirst] = a.split(', '); // expected and got ["Bernhard", "Sandra"]
const [bLast, bFirst] = b.split(', '); // expected and got ["Bethea", "Erin"]
return aLast > bLast ? 1 : -1;
})
Yes. If you look at the .sort() method on MDN it states the case when the compare function parameter is omitted:
Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.
Since Unicode is a super set of ASCII then yes it does sort in ASCII order.
Unless you are using "ASCII" like "Kleenex" (vs facial tissue), no, nothing in JavaScript is ASCII. In particular, strings are counted sequences of UTF-16 code units, one or two of which encode a Unicode codepoint. (This applies to Java, .NET, JavaScript, HTML, XML,….)
A lexicographic ordering is not very useful for human-valued text. Since you ask about alphabetically sorting, you must have a particular language's writing system's alphabet in mind. So, perhaps you want to pass localeCompare to sort.
you would have to do something like this:
objArray.sort(function(a, b) {
var textA = a.DepartmentName.toUpperCase();
var textB = b.DepartmentName.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
note: changing the case (to upper or lower) ensures a case insensitive sort.
To support unicode:
objArray.sort(function(a, b) {
return a.DepartmentName.localeCompare(b.DepartmentName);
});
Example