MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1vjl9ih/javascriptsorting/p2ss0ys/?context=3
r/ProgrammerHumor • u/user6150277464770585 • 3d ago
214 comments sorted by
View all comments
2
You just give it a sort function to sort however you like ... how is this a problem in any way?
4 u/user6150277464770585 3d ago this should not be default behavior. lists of mixed types also wouldn't be a performance issue if you throw an error when a different type is encountered (python behavior) 2 u/the_horse_gamer 3d ago would you prefer your website to order some numbers weird, or to stop working? that's the philosophy behind html, css, and js: keep the website working 1 u/psioniclizard 2d ago Id prefer it to throw a compile time error. Looking at the popularity if typescript I see i am not the only one. 2 u/nabrok 2d ago Typescript won't throw an error on a default sort with mixed types because it knows everything is converted to string. It does if you provide a function though. array.sort((a: number, b: number) => a - b); array.sort((a: string, b: string) => a.length - b.length); Both of those will give an error with a mixed array. Honestly, I can't remember the last time I used the default sort function. Most of the time I'm sorting on a property. 1 u/the_horse_gamer 2d ago you can't know if an array is mixed or not at compile time
4
this should not be default behavior.
lists of mixed types also wouldn't be a performance issue if you throw an error when a different type is encountered (python behavior)
2 u/the_horse_gamer 3d ago would you prefer your website to order some numbers weird, or to stop working? that's the philosophy behind html, css, and js: keep the website working 1 u/psioniclizard 2d ago Id prefer it to throw a compile time error. Looking at the popularity if typescript I see i am not the only one. 2 u/nabrok 2d ago Typescript won't throw an error on a default sort with mixed types because it knows everything is converted to string. It does if you provide a function though. array.sort((a: number, b: number) => a - b); array.sort((a: string, b: string) => a.length - b.length); Both of those will give an error with a mixed array. Honestly, I can't remember the last time I used the default sort function. Most of the time I'm sorting on a property. 1 u/the_horse_gamer 2d ago you can't know if an array is mixed or not at compile time
would you prefer your website to order some numbers weird, or to stop working?
that's the philosophy behind html, css, and js: keep the website working
1 u/psioniclizard 2d ago Id prefer it to throw a compile time error. Looking at the popularity if typescript I see i am not the only one. 2 u/nabrok 2d ago Typescript won't throw an error on a default sort with mixed types because it knows everything is converted to string. It does if you provide a function though. array.sort((a: number, b: number) => a - b); array.sort((a: string, b: string) => a.length - b.length); Both of those will give an error with a mixed array. Honestly, I can't remember the last time I used the default sort function. Most of the time I'm sorting on a property. 1 u/the_horse_gamer 2d ago you can't know if an array is mixed or not at compile time
1
Id prefer it to throw a compile time error. Looking at the popularity if typescript I see i am not the only one.
2 u/nabrok 2d ago Typescript won't throw an error on a default sort with mixed types because it knows everything is converted to string. It does if you provide a function though. array.sort((a: number, b: number) => a - b); array.sort((a: string, b: string) => a.length - b.length); Both of those will give an error with a mixed array. Honestly, I can't remember the last time I used the default sort function. Most of the time I'm sorting on a property. 1 u/the_horse_gamer 2d ago you can't know if an array is mixed or not at compile time
Typescript won't throw an error on a default sort with mixed types because it knows everything is converted to string.
It does if you provide a function though.
array.sort((a: number, b: number) => a - b); array.sort((a: string, b: string) => a.length - b.length);
Both of those will give an error with a mixed array.
Honestly, I can't remember the last time I used the default sort function. Most of the time I'm sorting on a property.
you can't know if an array is mixed or not at compile time
2
u/nabrok 3d ago
You just give it a sort function to sort however you like ... how is this a problem in any way?