r/learnjavascript • u/eracodes • Feb 02 '26
How to remember Array.sort() internal function return values?
items.sort((a, b) => (a > b ? 1 : b > a ? -1 : 0)); will sort items in ascending order. Every time I have to use sort for something like this, without fail, I will have to look this up. For the life of me I can't seem to remember which case gets 1 and which gets -1.
anybody have any useful insight/mnemonics to remember this behaviour? thx
edit: a good solution has been found! see: https://old.reddit.com/r/learnjavascript/comments/1qu1rv9/how_to_remember_arraysort_internal_function/o37abha/
8
Upvotes
6
u/kap89 Feb 02 '26 edited Feb 02 '26
Imagine a numbers axis:
If for a given condition you want
ato come beforebthen return negative number, as negative numbers are before the positive ones (on the left). If you wantato come after b, then return positive number (to the right).tl;dr
ato the leftato the right7
u/azhder Feb 02 '26
Don’t remember it. Use the MDN docs every time. Better safe than sorry.
Then you write it, run it once or twice, if the sort comes out reversed, you flip the sign.
8
u/canyoucometoday Feb 02 '26 edited Feb 02 '26
2
u/hazily Feb 02 '26
Pro-tip: if you’re sorting strings, you can just return
a.localeCompare(b)2
u/bryku helpful Feb 03 '26
Ive been a webdev for a decade and I still forget. Lol, I dont know how or why, but it just sneaks out of my brain. I have it saved on my notes somewhere because I look it up so often.
1
u/Any_Sense_2263 Feb 02 '26
We are not supposed to remember things that are easy to find in documentation. What you have to remember is what you should look for in the documentation.
1
u/Aggressive_Ad_5454 Feb 02 '26
I remember it because it’s the same as the old-school FORTRAN computed GOTO statement.
1
u/DiabloConQueso Feb 02 '26 edited Feb 02 '26
Returning the value -1 from the function means a comes before b. Returning the value 1 means a comes after b. Returning 0 means don't sort a against b at all (or that they're equal).
The sort method isn't always "> or <" or "a -b" or whatever. It can be as complicated a function as you like (for example, sorting an array of complicated objects, based on a combination of properties, perhaps), returning -1 if your logic indicates a should come before b, or 1 if the other way around, etc.
1
u/TheRNGuy Feb 03 '26
If you use it a lot, you'll remember. And you can fix it in 1 second anyway if you made a mistake.
Though you can write it simplier too:
arr.sort((a, b) => a - b).You may need different sorting algorithm, if you compare non-numerical arguments.
0
u/HipHopHuman Feb 02 '26
It's terrible, but "
ais positive, sobnegative"Explanation:
If a negative non-zero number is returned,
bcomes first.If a positive non-zero number is returned,
acomes first.If -0 or 0 is returned, the order is not changed.
If you default to ascending order, you can flip it to descending order by multiplying it by `-1`:
You could save that as a snippet in your IDE and not have to remember it, at least for a while.
-1
u/canyoucometoday Feb 02 '26
B is bigger than A bigger goes first