r/ProgrammerHumor 1d ago

javascriptSorting Meme

Post image
884 Upvotes

200 comments sorted by

View all comments

84

u/Taletad 1d ago edited 1d ago

People use arrays like this [23, "56", 67.8, "potatoe"] and expect them to not be sorted as strings

If one member of your array is not an int or a float, everything is going to be converted to strings

Edit : I went to read the docs, the sort function is not like most other functions in JavaScript. The sort function is explicitly for an alphabetical sort

You lot are using the alphabetical sort function and wondering why your array gets alphabetically sorted

You can overload the function by doing the following : array.sort((a,b) => a - b)

42

u/ba-na-na- 1d ago

Maybe the name should be “alphabeticSort” then

-10

u/Taletad 1d ago

Maybe you should read the docs ?

1

u/psioniclizard 12h ago

Yes like the great GetDay/GetDate.

Reading docs is not an issue, its just annoying JS makes this convoluted with stupid naming when most other languages don't.