Can javascript please get a decent inbuilt sorting method?!

Like, wtf is this shit?
[1, 3, 2, 12, 24, 5, 17].sort() === [1, 12, 17, 2, 24, 3, 5]

I'm fucking tired of having to reinvent the wheel every fucking time

  • 5
    I think .sort() by default converts the array's elements into string
  • 4
    String sorting - aka magic sort.

    What you're looking for is below
  • 7
  • 4
    @C0D4 agreed, I don't usually try and shit on OP but knowing that JS has alphabetical and numerical sort on the same method is pretty common knowledge.

    It doesn't come close to our all powerful all knowing and encompassing php array sort 🥴 which took me a little over an entire month to understand that the thing would completely sort an array in place and yo boy was assigning the output of a sort to a variable.

    I is genius
  • 1
    @AleCx04 don't we all do that at some point.

    You feel like an total jackass when you work it out though 😅
  • 0
    @C0D4 I did but thankfully I was just starting with wev development and php. Now a days if something like that happens I would crawl into a ball in my office and my sraff wod have to force the door open to give me chocolate or something
  • 1
    ints.sort((a, b) => a - b)
  • 1
    @kiki ...but that won't work with IE6!
  • 3
    @AlmondSauce but I don’t care...
  • 0
    @kiki Tounge in cheek. That's the usual response I get whenever I give ES6+ syntax as an example.
  • 0
    @AlmondSauce Why would you care about IE10- these days tho?

    @10Dev you know you can use: @highlight yourArray.sort((a, b) => a-b)
  • 0
  • 2
    imo it doesn't matter that people that know JS know about this quirk, the point of this rant is that it's not a good design..

    just think about it... the sort function takes your input, changes its type completely, and then sorts it on the data it made up...

    I'd rather see it use named sorts like strSort, intSort or something... put it into it's respective type classes I don't care, but don't magically assume my input is something it's not when using a commonly named function like a generic "sort"... if your language doesn't have strong typing you shouldn't act like it has... you should design it in a way that makes it actually intuitive, or go the python way, and do type detection at function level...

    this is bullshit and I dare you to defend this design...

    pro tip: you can't
  • 1
  • 0
    What made you think you can compare two arrays directly?
Add Comment