Timeline for Sort array by firstname (alphabetically) in JavaScript
Current License: CC BY-SA 4.0
30 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 3 at 10:20 | comment | added | Alonso Urbano |
This does not work with typescript: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. Also same error for right-hand side.
|
|
| Jun 8, 2022 at 10:39 | history | edited | Nenad Zivkovic | CC BY-SA 4.0 |
added 1 character in body
|
| May 29, 2021 at 4:31 | history | rollback | user229044♦ |
Rollback to Revision 7
|
|
| May 21, 2021 at 20:07 | history | edited | dthree | CC BY-SA 4.0 |
added example
|
| Nov 20, 2019 at 21:54 | comment | added | RiaD | wut? Code after return will not execute, so there's no difference. | |
| Nov 20, 2019 at 18:38 | comment | added | Pipo |
your sort would be more efficient with an else if than with 2 tests if > AND if < one excluding the other on each loop pass
|
|
| Jun 6, 2019 at 10:27 | comment | added | Nishant | It is not working with the last record comes under array. It sort all the data perfect but doesn't sort last data. In my country list, Canada is coming at last (In array also, Canada is the last value). Please help to fix this. | |
| Mar 28, 2019 at 17:06 | comment | added | Kanan Farzali | it is buggy as won't work for string with different cases. You need to lowercase as well comparison strings | |
| Oct 21, 2018 at 9:36 | history | edited | Stefan Falk | CC BY-SA 4.0 |
added 8 characters in body
|
| Sep 20, 2016 at 7:18 | history | rollback | RiaD |
Rollback to Revision 4
|
|
| S Sep 19, 2016 at 23:30 | history | suggested | CommunityBot | CC BY-SA 3.0 |
Add localeCompare example
|
| Sep 19, 2016 at 19:42 | review | Suggested edits | |||
| S Sep 19, 2016 at 23:30 | |||||
| Aug 8, 2016 at 10:40 | comment | added | RiaD | @Nisha, check firstname first, if they not equal return appropriately, if the equal, compare ids | |
| Aug 8, 2016 at 9:03 | comment | added | Nisha | What if a numerical field needs to be included along with the string field? @RiaD Suppose firstname + id. But id 10 should not be less than 5 as string | |
| Jan 8, 2016 at 17:07 | comment | added | RiaD | @commenters: Rewritten answers, added mention of "correct" sorting methods | |
| Jan 8, 2016 at 17:06 | history | edited | RiaD | CC BY-SA 3.0 |
added more explanation and link to another answers
|
| Sep 14, 2015 at 13:35 | review | Suggested edits | |||
| Sep 14, 2015 at 13:37 | |||||
| Jun 15, 2015 at 17:57 | history | edited | RiaD | CC BY-SA 3.0 |
user -> users
|
| Jun 15, 2015 at 12:21 | comment | added | jscripter | Are you applying sort to the object user, or array? array should be named users for better understanding | |
| Apr 30, 2015 at 13:55 | comment | added | SpYk3HH |
users.sort(function(a, b) { return a.firstname < b.firstname ? -1 : a.firstname > b.firstname ? 1 : 0; })
|
|
| Jan 9, 2014 at 13:08 | comment | added | MarzSocks | Dont forget to use the same text casing (e.g. 'toLowerCase()' ). | |
| Aug 9, 2013 at 6:25 | comment | added | candieduniverse |
In ternary: users.sort(function(a, b) { return (a > b ? 1 : (a === b ? 0 : -1)) })
|
|
| Jul 6, 2013 at 5:47 | comment | added | Spoike |
This code will only work on english speaking countries. In other countries you should be using ovunccetin's answer with localeCompare.
|
|
| Jul 6, 2013 at 5:04 | history | edited | RiaD | CC BY-SA 3.0 |
added 5 characters in body
|
| Apr 1, 2013 at 14:19 | comment | added | mlienau | For those coming in at a later date, Mrchief's answer is better because it's case insensitive. | |
| Jan 30, 2013 at 19:16 | comment | added | AlbertEngelB | Who would have thought strings could be sorted just by their value? Sweeeeet. | |
| Nov 16, 2012 at 10:30 | review | Suggested edits | |||
| Nov 16, 2012 at 10:37 | |||||
| Jul 4, 2012 at 21:31 | review | Suggested edits | |||
| Jul 5, 2012 at 2:01 | |||||
| Jul 15, 2011 at 21:54 | vote | accept | Jonathan Clark | ||
| Jul 15, 2011 at 19:15 | history | answered | RiaD | CC BY-SA 3.0 |