JavaScript string sort localecompare () notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.