The difference between sort (), sort (function (a, b) {return ab;}); and sorting (function (a, b) {...})

I am trying to understand exactly how sort () works and how I should use it.

I did some research (google) and looked at similar questions here on stackoverflow, but there are a few more things that are 100% clear to me.

So, so far I understand the following:

There is:

sort () without parameters : sorts only simple arrays String values in alphabetical order and ascending order

eg.

// sort alphabetically and ascending: var myArr=["Bob", "Bully", "Amy"] myArr.sort() // Array now becomes ["Amy", "Bob", "Bully"] 


sort () with a function as a parameter : sorts objects in arrays according to their properties; elements, however, are compared as numbers

 myArr.sort(function(a,b) { return a - b; }); 


sort () with a function as a parameter : sorts objects in arrays according to their properties; items can be numbers or strings

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


I tried to sort the next array with all these 3 sort () functions.

 var myArr = [{ "sortnumber": 9, "name": "Bob" }, { "sortnumber": 5, "name": "Alice" }, { "sortnumber": 4, "name": "John" }, { "sortnumber": 3, "name": "James" }, { "sortnumber": 7, "name": "Peter" }, { "sortnumber": 6, "name": "Doug" }, { "sortnumber": 2, "name": "Stacey" }]; //myArr.sort(); // doesn't do anything since it doesn't know on what property to sort /* myArr.sort(function(a, b) { return (a.sortnumber - b.sortnumber); // sorts array return (a.name - b.name); // doesn't sort array }); */ /* // sorts array even when I use name as property to sort on myArr.sort(function(a, b) { if (a.sortnumber < b.sortnumber) return -1; else if (a.sortnumber > b.sortnumber) return 1; return 0; }); */ console.log(myArr); 

There is also a violin.

So my questions are:

  • Do I understand correctly?
  • Is there something I am missing?
  • If the third case always works, can I always stick to it or the other two cases are more effective in some way or have any advantages for the third case?

I would really appreciate it if anyone could dwell on this in more detail. Thanks.

+5
source share
4 answers

So, after some further research, going through the MDN and arraysort and arraysort2 documentation that I found very useful, I created a slide that could probably be useful to someone else, so I post it here. Thank you all for your answers!

enter image description here

+3
source

First of all, you did a good study and looked at almost every possible case, and you can find the MDN documentation here

You just missed the case of Sorting non-ASCII characters

To sort strings with non-ASCII characters, i.e. strings with accented characters (e, é, è, a, ä, etc.), strings from languages ​​other than English: use String.localeCompare. This function can compare these characters so that they appear in the correct order.

 var items = ['réservé', 'premier', 'cliché', 'communiqué', 'café', 'adieu']; items.sort(function (a, b) { return a.localeCompare(b); }); // items is ['adieu', 'café', 'cliché', 'communiqué', 'premier', 'réservé'] 
+2
source

According to the Array link ( http://www.w3schools.com/jsref/jsref_sort.asp ):

By default, the sort() method sorts the values as strings in alphabetical and ascending order.

So your first understanding of sort() correct. However, the second and third are still not true. First of all, these are both cases that provide a sort function to the sort() method. This method should compare the values ​​of a and b and return negative, zero, or positive values, indicating that a less than, equal to, or greater than b . For example, you can compare your myArr using the name property as follows:

 myArr.sort(function(a,b) { return a.name.localeCompare(b.name); }); 
+1
source

I think you would like to combine the sorting criteria, like this example, which sorts by name forst and then by number. Please see 'John' .

 var myArr = [{ "sortnumber": 9, "name": "Bob" }, { "sortnumber": 5, "name": "Alice" }, { "sortnumber": 4, "name": "John" }, { "sortnumber": 3, "name": "James" }, { "sortnumber": 7, "name": "Peter" }, { "sortnumber": 6, "name": "Doug" }, { "sortnumber": 2, "name": "Stacey" }, { "sortnumber": 14, "name": "John" }, { "sortnumber": 12, "name": "John" }]; myArr.sort(function (a, b) { return a.name.localeCompare(b.name) || a.sortnumber - b.sortnumber; }); console.log(myArr); 
+1
source

All Articles