![sort in javascript sort in javascript](https://www.developerhelps.com/wp-content/uploads/2020/07/read-1.png)
All the other inner values just ‘come along for the ride’, as it were, and this way we can sort arrays containing a mixture of values. This is how we sort multi-dimensional arrays, using a value from each inner array as the sort criterion. If a and b are themselves arrays, well, directly comparing arrays using mathematical evaluation won’t produce the results we want but we can compare their inner values and do the sort with them. We also assign the results of those operations to new variables, because some browsers object to having the arguments over-written. Note how we pre-convert each of the strings to lower-case, which ensures we get a case-insensitive sort (if we didn’t do that then lower and upper-case letters would be sorted separately). Var x = a.toLowerCase(), y = b.toLowerCase()
![sort in javascript sort in javascript](https://miro.medium.com/max/1400/1*4513bQIw7OwWq5rbIWE7NA.png)
We can also create a comparison function that produces a dictionary sort, by defining three comparisons to evaluate each pair of strings - in computational terms, "a" is less than "b", so we can directly compare the strings like that, to then return one of the three sorting values: So inverse-numerical order can be produced simply by reversing the equation: We can work through that with value examples: since a = 8 and b = 5, then (a - b) = 3 three is greater than zero, so b will be sorted before a, producing the order. For example, if the function returns (a - b), then that will produce a numerical sort: So the normal way of using the comparison function, is to perform and return a simple evaluation which produces the desired sort. It’s using the word “index” in a very confusing way how I’ve expressed the conditions above should hopefully be much clearer. But what this actually means is, “sort b lower down the list than a”, which in terms of numerical indexing is a higher, not lower index. The JavaScript specification refers to the first sort-condition as sort b to a lower index than a. The specification defines the rules in a confusing way if the function returns zero, leave a and b unchanged with respect to each other.if the function returns greater than zero, sort b before a.if the function returns less than zero, sort a before b.The comparison function itself accepts two arguments, usually referred to as a and b, which represent the two values being compared in each operation. Otherwise the argument to sort is a comparison function, which defines the sort behavior according to how it returns. If sort() is called with no argument, then the array is sorted lexicographically - in dictionary order, when each value is treated as a string:Īlert(letters) //produces
![sort in javascript sort in javascript](https://res.cloudinary.com/practicaldev/image/fetch/s--iBK-zqJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/yi1c9sina9zbg56a9ybq.png)
→ If you already know the basics then you might want to skip ahead. Using sort it’s possible to organise arrays, not just into alphabetical or numerical order, but into any bespoke arrangement we can express in conditional logic. JavaScript’s sorting mechanism is a model of simplicity, yet bubbling under the surface is some incredibly flexible and powerful functionality.