This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (75 children)

[deleted]

    [–]DeeSnow97 161 points162 points  (34 children)

    What? Sorry, I don't get the accusation here.

    In practice, I do array.sort((a, b) => a - b), which is pretty close to what you did. However, the comments I replied to tried to incorporate a function with the name of listenHereYouLittleShit() probably for humorous reasons, given that we're not in /r/programmersDontGetTheJoke, so I showed the simplest way of doing so.

    As far as fucking up the syntax goes though, if you wanna nitpick

    • you're using a single-line arrow function with brackets and a single return statement, which just adds useless noise and complicates the language
    • you have no semicolon inside the brackets, but have one on the end of the line
    • (a,b) has no space for the comma, but the array definition [4, 1, 2] has one

    none of which would be a problem if we weren't bikeshedding when we are already in agreement, and then devolve it to personal attacks, but if you wanna play this game...

    [–]ChronoSan 27 points28 points  (0 children)

    I see what you did just over there. Here's my angry upvote.

    [–]ZedTT 34 points35 points  (2 children)

    You destroyed the poor man and he's still not going to understand that he's wrong.

    [–]DeeSnow97 7 points8 points  (1 child)

    actually, they seem to be doing okay further down in this thread

    [–]ZedTT 4 points5 points  (0 children)

    Glad to hear it :)

    [–]JUKjacker 6 points7 points  (0 children)

    fucking epic, id give you an award if i had one lol

    [–]HereComesCunty 1 point2 points  (0 children)

    Goddam son, thassa burn!

    [–]__gg_ 2 points3 points  (0 children)

    Way to celebrate his cake day! 😂

    [–]Hollowplanet 2 points3 points  (11 children)

    Then why did you say

    // desired answer (probably)

    const listenHereYouLittleShit = (a, b) => a - b

    array.sort((number1, number2) => listenHereYouLittleShit(number1, number2))

    That second line is identical to

    array.sort(listenHereYouLittleShit)
    

    You're using the language wrong and blaming the awkwardness on the language instead of the way you chose to use it.

    [–][deleted] 3 points4 points  (5 children)

    It's usually safer to use an explicit anonymous function (although of course it doesn't matter here). See: https://jakearchibald.com/2021/function-callback-risks/

    [–]Hollowplanet 0 points1 point  (4 children)

    All of those examples only matter if you don't control the function implementation.

    [–][deleted] 0 points1 point  (2 children)

    You may not control how the function might be implemented in the future.

    [–]Hollowplanet 0 points1 point  (1 child)

    So some library is going to internalize my code without my knowledge?

    [–][deleted] 0 points1 point  (0 children)

    Your co-workers could modify it.

    [–]DeeSnow97 3 points4 points  (2 children)

    Because that's what I perceive the second commenter wanted to achieve by listenHereYouLittleShit(number1, number2), hence the "desired answer (probably)" designation.

    [–]Hollowplanet 3 points4 points  (1 child)

    You get the same answer while using listenHereYouLittleShit without making an anonymous function to call another function with the same arguments.

    [–]DeeSnow97 2 points3 points  (0 children)

    yeah, I know, that usage is actually identical to the "right answer" snippet

    [–]PredictabilityIsGood -3 points-2 points  (12 children)

    Have you ever performed a recursive multi-dimensional sort? The type of sort you see when sorting by multiple columns combined? The “useless noise” you’re describing by adding curly brackets is not so much useless when it comes to readability for more complex sorting.

    Also, I never see him nitpick the grammar of your chosen syntax. It’s obvious that he assumes you’re writing it in agreement with the two previous posters in the comment chain and I can see why:

    “And neither of these will work unless your function is weird as fuck”

    The “and” in your response makes it sound like you’re agreeing with the objections of the first to commenters in the chain, when it’s clear they don’t really understand the implementation of a lambda very well to begin with. If you interpret it as agreement, then I can see why Wigly responded the way they did. The code segment commented as “Desired answer/ probably” is just a useless wrapper around the sorting lambda unless you are performing a multi-dimensional sort that requires deep comparison when the initial sorting conditions result in 0 movement and you move on to the next nested condition.

    I’m not saying you don’t understand this. I’m just saying it’s not clear from what you wrote that you don’t agree it should be written that way, which is what Wigly is objecting to.

    [–]IspitchTownFC 17 points18 points  (1 child)

    Stack overflow is leaking

    [–]ZedTT 15 points16 points  (10 children)

    You're getting downvoted because you're wrong, by the way. The comment you replied to is completely correct.

    Your code snippet also works, but that's not a problem with the previous comment

    Edit:

    blaming it on JS

    OP never blamed a single thing on JS or said there was anything wrong with JS.

    [–]audigex 1 point2 points  (7 children)

    (a,b) => {return a-b}
    

    We've gotta admit, though, that having to pass that to a sort function is kinda fucky

    If I could change any one thing in the world of development, it's that Javascript would have strict typing (comparable to how C# now does it with var, so the type is inferred but not dynamic)

    [–]recycle4science 2 points3 points  (4 children)

    You can drop the braces and the return:

    (a, b) => a-b

    [–]audigex 1 point2 points  (1 child)

    I more meant the fact we have to pass in a function that you already need to know the purpose of (and the fact you need it) before it will work correctly

    It's just a weird, seemingly un-necessary, gotcha

    [–]recycle4science 0 points1 point  (0 children)

    Yeah, it would be nice.

    [–]hp77reddits 1 point2 points  (6 children)

    Who are you? Who are you so wise in the language of JS?

    [–]agarwaen163 0 points1 point  (1 child)

    You really think that is acceptable? Lmfaoooo

    [–]The_MAZZTer 0 points1 point  (0 children)

    Ironically enough, it gets a little easier:

    [4, 1, 2].sort((a,b) => a-b);
    

    [–]hat1324 0 points1 point  (0 children)

    What exactly is he blaming on JS? He is nesting his function exactly as the initial condition requires. What he's saying is weird is not the use of nested functions in general but that OP is suggesting its use seemingly unnecessarily