0

Can someone explain to me why this code is working ?

[1,4,5,6,7,2,3]["sort"](0); //[1, 2, 3, 4, 5, 6, 7]

I'm not quite understanding the syntax here. Meanwhile, why can't I use like this to choose sort or reverse to execute? Looks like it always goes to do reverse no matter what the index is.

[1,4,5,6,7,2,3]["sort","reverse"](0); 
Johnny Chen
  • 2,025
  • 1
  • 18
  • 27

6 Answers6

6

Let's break down your code step by step.

[1,4,5,6,7,2,3]

This is an array literal. That is, to write [1,4,5,6,7,2,3] is a bit like saying new Array(1,4,5,6,7,2,3). Simply put, the array literal evaluates to a new array with the described contents.

[1,4,5,6,7,2,3]["sort"]

Now this bit is interesting, because knowing [] creates array literals makes us think ["sort"] is also an array literal. Not quite - this is an example of Javascript bracket notation, where we access the properties of a preceding object with the syntax someObject["propertyName"], equivalent to someObject.propertyName. Check out this SO answer for more info on bracket notation.

So we know the sort property of Arrays, Array.sort, is a function. To write [1,4,5,6,7,2,3]["sort"] is like saying ([1,4,5,6,7,2,3]).sort. To invoke a function, we use parentheses with the arguments we want:

[1,4,5,6,7,2,3]["sort"]();

This is like saying ([1,4,5,6,7,2,3]).sort();.

Now in your code, you've passed in a 0 as the argument for Array.sort. However, this 0 actually has no effect on the result of sorting! That's because the sort method for arrays takes an optional comparison function. As 0 is not a function, Array.sort will simply ignore it. In fact, in the Firefox console, running [1,4,5,6,7,2,3]["sort"](0); gives us a TypeError: invalid Array.prototype.sort argument.

Putting it all together, we can reverse our result with the following:

[1,4,5,6,7,2,3]["sort"]()["reverse"]();

Equivalent to:

([1,4,5,6,7,2,3]).sort().reverse();
heartyporridge
  • 1,151
  • 8
  • 25
2

What you are looking for is probably

[1,4,5,6,7,2,3]["sort"]();

and

[1,4,5,6,7,2,3]["sort"]()["reverse"]();

(another alternative at the end of this answer)


When you actually do this

[1,4,5,6,7,2,3]["sort","reverse"](0);

what's happening is first the "sort","reverse" is evaluated (see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Comma_Operator) and retuns "reverse". So effectively you have

[1,4,5,6,7,2,3]["reverse"](0);

which is

[1,4,5,6,7,2,3].reverse(0);

While the reverse method doesn't take any arguments, like all Javascript functions it doesn't mind if you pass in extra parameters (which it ignores). So that's effectively

[1,4,5,6,7,2,3].reverse();

which returns [3, 2, 7, 6, 5, 4, 1]


What you probably want is

[1,4,5,6,7,2,3][["sort","reverse"][0]]();
[1,4,5,6,7,2,3][["sort","reverse"][1]]();

which calls sort and reverse respectively

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
1

Any member of an object in JavaScript can be accessed using its name in square brackets. You accessed the function "sort" this way and then you invoked it. JavaScript also doesn't care if you provide an unused argument to a function. It will just ignore the argument.

If you try to provide a list of strings in order to access a member, JavaScript might do something odd just to be able to make a decision and recover. Don't use a list on it. Just invoke the first function, which returns the array, then invoke the second function.

The cleanest way (which you may know already) is to simply write myList.sort().reverse().

Rikki Gibson
  • 4,136
  • 23
  • 34
1

As a rule, JavaScript will let you address any attribute of an object (which in JS is a class, which in turn is a special-case function if you're using ES5 semantics, which you most likely are) using either dot notation (classname.funcname()) or using bracket notation/a JSON key (classname['funcname']()).

This has to do with how JS structured objects before ES6, and a long and interesting talk can be had about how higher-order functions and lambdas and closures play into this but that's a digression.

Jules
  • 14,200
  • 13
  • 56
  • 101
0

think about the array as an object that contain property sort.

you pass 0 to the function but the function not use this number

[1,4,5,6,7,2,3]["sort"](0); //[1, 2, 3, 4, 5, 6, 7]

this is the same as

var a=[1,4,5,6,7,2,3];
a.sort();

and

[1,4,5,6,7,2,3]["sort","reverse"](0); 

is the same as

var a=[1,4,5,6,7,2,3];
a.reverse();

think about the array as an object that contain property sort. you take t

Alon
  • 2,919
  • 6
  • 27
  • 47
-1

Its array built in prototype. You dont have to pass any arguments.

check thess documents. Reverse prototype , Sort prototype

Raj
  • 103
  • 5