Turn: http://www.cnblogs.com/anniey/p/6184301.html
Today, I encountered a non-contact method, which is Localecompare (). Check out some of the relevant documents, the definition is vague, so write down your own understanding.
Let's start with an example!
A string of student names that is known, sorted in descending order according to the length of the student's name, and returns a list of names. The names of equal lengths are returned in reverse alphabetical order (z-> A).
Given string = "Xxa xxb xxc xxd xa xb xc xd";
return [' Xxd ', ' xxc ', ' xxb ', ' xxa ', ' xd ', ' xc ', ' xb ', ' xa '];
Next is the code of the positive solution!
1 function lineupstudents (students) {2 var stu=students.split (""); 3 stu.sort (function (b) {4 if (a.length==b.length) {5 return B.localecompare (a); 6 }else{7 return b.length-a.length; 8 } 9 }); Console.log (Stu);}12 var s1 = ' Tadashi Takahiro Takao Takashi Takayuki Takehiko Takeo Takeshi T Akeshi '; lineupstudents (S1);//Output [' Takehiko ', ' Takayuki ', ' Takahiro ', ' Takeshi ', ' Takeshi ', ' Takashi ', ' Tadashi ', ' Takeo ', ' Takao ']
In this example, the sorting method of the array object is used, and then the local sorting is done in the function provided by the sort parameter.
Next look at the sort () official saying:
Arrayobj. Sort (sortfunction)
Parameters
Arrayobj
Required option. Any Array object.
Sortfunction
Options are available. is the name of the function used to determine the order of the elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.
Description
The sort method sorts the array objects appropriately, and does not create a new array object during execution.
If you provide a function for the sortfunction parameter, the function must return one of the following values:
- A negative value, if the first argument passed is smaller than the second one.
- 0, if two parameters are equal.
- Positive value if the first argument is larger than the second argument.
Let's see what Localecompare () is saying!
Definition and Usage
Compares two strings in a local-specific order.
Grammar
Stringobject.localecompare (target)
Parameters |
Describe |
Target |
The string to compare with Stringobject in a local-specific order. |
return value
A number that describes the result of the comparison. If Stringobject is less than target, Localecompare () returns a number less than 0. If Stringobject is greater than target, the method returns a number greater than 0. If two strings are equal, or if there is no difference based on the local collation, the method returns 0.
A description of the two methods, in which the sortfunction parameter exists, will have a numeric result return value. The most critical step in the example is B.localecompare (a), which compares two strings in a particular order;
In addition, Stringobj.localecompare, who compares who in target, determines whether the list is ascending or descending. For example:
1 if (a.length==b.length) {2 return a.localecompare (b); 3}else{4 return a.length-b.length;5}
The results are displayed in ascending order!
For Localecompare () (compare two strings, consider the default local collation), the local rules used are Chinese and English, and in the case of English, they are sorted alphabetically. If it is useful to Chinese characters, it is sorted according to Chinese pinyin.
JavaScript string sort localecompare () notes