Brainlet here

can someone explain to me how this works?
myArr = ["JANE", "ADAM", "AARON", "BETTY", "YUSEF"];

myArr.sort(function(a, b){
if (a < b) {return -1};
if (a > b) {return 1};
});

//myArr is now sorted in alphabetical order

Attached: 1482457460067.jpg (412x985, 270K)

Other urls found in this thread:

youtube.com/watch?v=kPRA0W1kECg
twitter.com/NSFWRedditImage

BRRRRRRAAAAAAAAAAAAAAAAAAAP

Attached: 1528320983591.jpg (342x311, 19K)

What about it confuses you, friend? Which parts do you understand?

if only we had a thread for questions like this

friend
why is words higher in the alphabet < words lower in the alphabet true
what's the difference between returning 1 and negative 1
how does that sort an entire array, if it's comparing each element.
why does
myArr.sort(function(a,b){ return a < b; });

not just work

1. Javascript compares strings by their alphabetical index.
2. The sort array prototype optionally takes a sorting function as an argument.
3. This function returns +1 if the value should be shifted left, -1 for right.
4. Technically you should allow for duplicate values and return 0 if an exact match is found.

myArr.sort((a, b) => {
return a < b ? -1 : a > b ? 1 : 0;
});

In JavaScript, string < string does a lexicographical (alphabetical) comparison.

The function passed to .sort should return -1, 0 and 1 depending on whether a comes before, is equal to, or comes after b.

There's no reason why it couldn't have worked the way you describe it, but then if the sorting algorithm cared about less-or-equal vs merely less, it would have to call your function twice which would be slower.

it works fine
["JANE", "ADAM", "AARON", "BETTY", "YUSEF"].sort(function(a, b){
return a > b;
});

it's called a sort delegate ... tf

thanks bros it makes more sense now. What do 'a' and 'b' actually represent though? i thought it was going through the array, comparing 'a' each element to 'b' the element after it. Is that correct?

put a console.log in there and find out

dude thank you so much!!!!

I fucking love this board, don't let anyone give you shit Jow Forums

Attached: Capture.png (110x141, 3K)

>comparing 'a' each element to 'b' the element after it

Somewhat correct. A and B represent two values in the array, whichever are being currently compared, but not necessarily the adjacent value. There are many different sorting algorithms but the faster ones will not compare every value in a linear way. Here's a cool video to help you visualise this:

youtube.com/watch?v=kPRA0W1kECg

based

that's very cool

I'm not OP, but how many times does the sort function call its delegate? I would imagine a maximum of once for each distinct pair of array elements. That would give you each name's "value" relative to every other name in the array. In this case, the delegate would be invoked 4+3+2+1=10 times, if my math is correct. I feel like in certain situations you can get away with fewer invocations, though, like in a situation where you have 5 elements such that a

Yes, those are examples of worst-case and best-case sorts. Plenty of sorting algorithms exist to get the number of comparisons as close to best-case as possible under certain circumstances. It's a fairly safe bet that any JS implementation uses a fairly optimal sort.

That's got to be shopped

I reverse searched it with yandex and found an even worse photoshop, and two photos from the same set. Didn't try very hard to confirm if there's more.

Attached: psb.jpg (500x1158, 88K)

Attached: kaigai573014.jpg (771x1591, 178K)

Attached: ABAD830.jpg (1067x1600, 443K)

>anyone give you shit Jow Forums

Thanks. We get bullied a lot by Jow Forums

>pajeet's ill-conceived overloads are builtin in js
> meanwhile the type system is basically a distended anus when compared to other languages
>1.1234 '2'+2 *5 == "210"
>'penis' + null == "penisnull"
>people pretend this isn't a DSL in a google-colored gimp suit begging for death while being fed onions through a tube
>practically all the gore-tier software currently being produced are js webapps.
>peak of frontend webshit dev responsibility is following the w3c-jew's byzantine 'security' rules, no actual thought is put into correctly expressing designs in code.

Attached: 1508287667711.jpg (2209x2921, 1.08M)

Fpbp