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 Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › sort
Array.prototype.sort() - JavaScript - MDN Web Docs
The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
Discussions

How can I sort a Javascript array alphabetically? - Stack Overflow
I want to Sort the people alphabetically by last name. I'm curious if there's a more professional approach to doing that. My Array : const people = [ 'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Ca... More on stackoverflow.com
🌐 stackoverflow.com
Sort an Array Alphabetically using the sort Method
Tell us what’s happening: Could someone explain to me why this passed the challenge. I referenced the examples given and I’m curious as to why the way they wrote it out didnt work for me and this way did. Your code so… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
19
0
June 21, 2018
Sort array alphabetically javascript - Stack Overflow
quick question. Does the .sort method in javascript automatically sort items in an array using ASCII order. I've looked at examples etc but haven't seen a definitive answer anywhere. Also what More on stackoverflow.com
🌐 stackoverflow.com
javascript - Sort objects in an array alphabetically on one property of the array - Stack Overflow
This sorts them alphabetically AND is case insensitive. It's also super clean and easy to read :D ... This is also the only answer which mentions 'sensitivity': 'base', which makes a big difference depending on your application according to MDN. 2020-07-03T05:57:57.62Z+00:00 ... Save this answer. ... Show activity on this post. Copy// Sorts an array ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › jsref › jsref_sort.asp
JavaScript Array sort() Method
The sort() method sorts the elements of an array. The sort() method sorts the elements as strings in alphabetical and ascending order.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-alphabetically-in-javascript
Sort Alphabetically in JavaScript – How to Order by Name in JS
November 7, 2024 - Sometimes you might have an array ... sort the users by their names. We can do this in JavaScript by using the sort() method directly or with the compare function....
🌐
Scaler
scaler.com › home › topics › sort array alphabetically in javascript
Sort Array Alphabetically in JavaScript - Scaler Topics
May 4, 2023 - In JavaScript, there are various methods for sorting the different data types such as strings and integers. For example, an array of objects can be sorted using the sort() method and then retrieved in alphabetical order. It is possible thanks to the alphabet's index values.
🌐
Favtutor
favtutor.com › articles › sort-alphabetically-javascript
Sort Array Alphabetically in JavaScript (with code)
January 30, 2024 - The built-in sort() method in JavaScript is used to sort arrays alphabetically in JavaScript.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Sort an Array Alphabetically using the sort Method - Curriculum Help - The freeCodeCamp Forum
June 21, 2018 - Tell us what’s happening: Could someone explain to me why this passed the challenge. I referenced the examples given and I’m curious as to why the way they wrote it out didnt work for me and this way did. Your code so far function alphabeticalOrder(arr) { // Add your code below this line return arr.sort(); return a - b; }; // Add your code above this line alphabeticalOrder(["a", "d", "c", "a", "z", "g"]); Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10...
🌐
DEV Community
dev.to › jenshaw › sorting-out-javascript-sort-4kbl
Sorting out Javascript Sort - DEV Community
July 4, 2019 - Until recently, I'd really underestimated how much Javascript's sort method can accomplish. It's a tool that can easily be taken for granted, especially in those circumstances when you can just call it on an array and, without any extra effort at all, watch it magically rearrange its elements in order as you expect it to. // Orders names alphabetically let myPets = ["Oreo", "Shortbread", "Peanut", "Souffie", "Tella"]; pets.sort(); // => [ 'Oreo', 'Peanut', 'Shortbread', 'Souffie', 'Tella' ] // Order numbers numerically let numbers = [3, 2, 1, 6, 5, 4]; numbers.sort(); // => [ 1, 2, 3, 4, 5, 6 ] // Don't stop reading here!
🌐
Programiz
programiz.com › javascript › examples › alphabetical-order
JavaScript Program to Sort Words in Alphabetical Order
The elements of an array are sorted using the sort() method. The sort() method sorts the strings in alphabetical and ascending order.
🌐
DevGenius
blog.devgenius.io › how-to-sort-string-alphabetically-in-javascript-fbbb221a1ff9
How to sort string alphabetically in JavaScript ? | by Pravin M | Dev Genius
November 2, 2024 - User Experience: Presenting data in a familiar order enhances user experience. JavaScript offers the Array.prototype.sort() method, which can be used to sort an array of strings alphabetically.
🌐
YouTube
youtube.com › watch
Array Sort JS - Javascript Sort Alphabetically and Sort Array of Objects Javascript - YouTube
Check out Filestack - https://www.filestack.com/?utm_source=MonsterlessonsAcademy&utm_medium=Leads Acquisition&utm_content=MonsterlessonsAcademy-YT-0330202
Published: May 3, 2022
🌐
LabEx
labex.io › tutorials › sort-array-alphabetically-28618
Sort Array Alphabetically in JavaScript | LabEx
Use Array.prototype.sort() to sort the array based on the given property. Use String.prototype.localeCompare() to compare the values for the given property. ... const alphabetical = (arr, getter, order = "asc") => arr.sort( order === "desc" ?
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-sort-words-in-alphabetical-order
JavaScript Program to Sort Words in Alphabetical Order - GeeksforGeeks
July 23, 2025 - We can simply pass the array of words to the sort() method to sort themconst sortedWords = wordsArray.sort(); alphabetically. ... Example: In this example, we will see the use of the sort() Method.
🌐
W3Schools
w3schools.com › js › js_array_sort.asp
JavaScript Array Sort
ES2023 added the toSorted() method as a safe way to sort an array without altering the original array.