Javacript in alphabetical order

Source: Internet
Author: User

Previously, a requirement was made to sort by the pinyin of the person's name, then NLS_SORT sorted in the background through the parameters of Oracle, and then returned the results to the front end. Now want to see if the front-end pairs of strings in alphabetical order what should be done? To find the information, the method is recorded as follows.

[‘张三‘,‘李四‘,‘王五‘].sort((a,=> a.localeCompare(b, ‘zh-Hans-CN‘,{sensitivity: ‘accent‘}))

localeCompareThe syntax for the function is as follows:

referenceStr.localeCompare(compareString[, locales[, options]])

Parameters
comparestring the string to compare
locales is optional. A string or array of strings that represents one or more languages or regions that conform to the BCP 47 standard.

zh-Hans-CN (Chinese written using the Simplified script as used in mainland China)

Returns a number that indicates whether the reference string precedes, follows, or is the same as the comparison string in the sort.

    • Returns 1 when the reference string is in front of the comparison string
    • Returns 1 when a reference string is followed by a comparison string
    • Simultaneous return 0

do not rely on a specific return value of 1 or 1. the values of positive and negative numbers that are returned between browsers (and between different browser versions) vary, because only the return value is required to be positive and negative in the Web specification, and no specific value is specified. Some browsers may return 2 or 2 or some other negative, positive value.

Reference

Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
https://segmentfault.com/q/1010000002546028

Javacript in alphabetical order

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.