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
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.
🌐
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.
Discussions

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
JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property
I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an array of books, and each book object has properties like 'title', 'author', and 'publicationYear'. How can I sort this array of book objects alphabetically by the ... More on community.esri.com
🌐 community.esri.com
September 18, 2023
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 Array Content Alphabetically?
Any suggestions for how to automatically sort the contents of an array so that the elements appear in alphabetical (or alphanumerical) order? More on community.tulip.co
🌐 community.tulip.co
18
2
October 4, 2023
🌐
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...
🌐
Esri Community
community.esri.com › home › developers › arcgis maps sdk for javascript
JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property - Esri Community
September 18, 2023 - I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an array of books, and each book object has properties like 'title', 'author', and 'publicationYear'. How can I sort this array of book objects alphabetically by the ...
🌐
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!
Find elsewhere
🌐
Mimo
mimo.org › glossary › javascript › sort
JavaScript Sort Method: Advanced Array Sorting Techniques
You can also sort arrays of strings alphabetically using the sort() method with default or custom compare functions, such as for case-insensitive sorting.
🌐
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.
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-sort-javascript-arrays-a-guide-16f2e9b18079
How to Sort JavaScript Arrays: A Guide | by Doug Hill | JavaScript in Plain English
September 21, 2021 - and so on. While sorting strings in an array is simple, it gets a little more confusing after that. Let’s walk through sorting an array alphabetically, then dive into sorting numerically and some of the gotchas you can run in to.
🌐
DEV Community
dev.to › fpaghar › donesort-an-array-of-objects-ways-in-javascript-48hl
Sort an Array of Objects Ways in JavaScript - DEV Community
March 24, 2024 - Optionally, the order parameter, which defaults to 'asc', allows us to control the sorting direction. This function alphabetical accepts an array arr, a getter function to extract the property for comparison, and an optional order parameter to specify the sorting direction.
🌐
Tulip
community.tulip.co › support, troubleshooting, & help
Sort Array Content Alphabetically? - Support, Troubleshooting, & Help - Tulip Community
October 4, 2023 - Any suggestions for how to automatically sort the contents of an array so that the elements appear in alphabetical (or alphanumerical) order?
🌐
Unreal Engine
forums.unrealengine.com › development › programming & scripting
How to sort an array alphabetically? - Programming & Scripting - Epic Developer Community Forums
March 10, 2016 - I have an inventory system in the game that I’m creating, and it works rather well. For sake of organization, I’d like to know how I’d be able to better organize it, as items are simply added to an array and displayed in the order that they’re picked up.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Sort an Array Alphabetically using the sort Method 5 - Curriculum Help - The freeCodeCamp Forum
March 24, 2019 - Tell us what’s happening: I’m incredibly stuck on this one. Have tried using the hints to get help, but nothing has helped me. I cannot figure out what in the world it is that I am doing wrong with this one, now. My air…
🌐
Educative
educative.io › answers › how-to-alphabetically-sort-an-array-of-strings-in-javascript
How to alphabetically sort an array of strings in JavaScript
To perform a simple alphabetical sort on an array of strings, we can use the sort method. This method sorts the elements of an array in place and returns the sorted array.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › sort words in alphabetical order
JavaScript Program to Sort Words in Alphabetical Order
November 12, 2024 - Explore practical examples that demonstrate sorting simple word lists as well as handling strings with multiple words. When dealing with an array of words, JavaScript's Array.prototype.sort() method offers a straightforward way to arrange words ...
🌐
Codemia
codemia.io › home › knowledge hub › sort array by firstname (alphabetically) in javascript
Sort array by firstname (alphabetically) in JavaScript | Codemia
January 2, 2025 - Sorting an array by firstname or any other object attribute in JavaScript can be implemented effectively using the sort() method. Understanding how to leverage compareFunction properly allows for flexible and powerful sorting mechanisms tailored to specific needs—whether it’s simple ...
🌐
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.
🌐
egghead.io
egghead.io › lessons › javascript-sort-an-array-alphabetically-or-numerically
Sort an Array Alphabetically or Numerically | egghead.io
[00:00] Array Sort sorts the elements in an array in place, and returns the same array. If we wanted to alphabetically sort this array of names, we could simply call items.sort, and then Log Items.
Published: October 8, 2015