当前位置:网站首页>JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
2022-07-06 21:09:00 【viceen】
js Sort according to the first letter of Chinese characters ( Province sorting ) or Sort according to English initials ——z~a Sort & a~z Sort
Example 1
let arr = [" Guizhou Province ", " Jiangsu Province ", " Jiangxi Province ", " Zhejiang Province ", " Sichuan Province ", " Anhui Province ", " Shandong Province ", " Shanghai ", " Hubei province ", " Fujian Province ", " Liaoning Province ", " Shanxi Province ", " Hebei Province ", " Qinghai Province ",
" Heilongjiang Province ", " Inner Mongolia Autonomous Region ", " Beijing ", " Henan province ", " Hunan province ", " Guangdong province, ", " Shaanxi Province ", " Jilin Province ", " Yunnan Province ", " Xinjiang Uygur Autonomous Region ",
" Chongqing ", " tianjin ", " Gansu Province ", " Ningxia Hui Autonomous Region ", " Hainan ", " Guangxi Zhuang Autonomous Region ", " Tibet Autonomous Region "];
let sortArray = arr.sort(function (str1, str2) {
return str1.localeCompare(str2, 'zh');
});
Print the results
[' Anhui Province ', ' Beijing ', ' Chongqing ', ' Fujian Province ', ' Gansu Province ', ' Guangdong province, ', ' Guangxi Zhuang Autonomous Region ', ' Guizhou Province ', ' Hainan ', ' Hebei Province ', ' Henan province ', ' Heilongjiang Province ', ' Hubei province ', ' Hunan province ', ' Jilin Province ', ' Jiangsu Province ', ' Jiangxi Province ', ' Liaoning Province ', ' Inner Mongolia Autonomous Region ', ' Ningxia Hui Autonomous Region ', ' Qinghai Province ', ' Shandong Province ', ' Shanxi Province ', ' Shaanxi Province ', ' Shanghai ', ' Sichuan Province ', ' tianjin ', ' Tibet Autonomous Region ', ' Xinjiang Uygur Autonomous Region ', ' Yunnan Province ', ' Zhejiang Province ']
Example 2
Realize the sorting of Chinese according to the first letter of Pinyin
js Provides sort() Method to sort the data in the array , But it only works for English , At this time, you need to customize the sorting rules
[' Zhang San ',' Li Si ',' Wang Wu '].sort((a, b) => a.localeCompare(b, 'zh-Hans-CN', {
sensitivity: 'accent'}))
Output
[' Li Si ',' Wang Wu ',' Zhang San ']
- sort() It defines a function to specify the sorting rules ,localeCompare() Method returns a number , Indicates whether the reference string is before or after the sort order , Or the same as the given string in the sort order ,
zh-Hans-CNIs the sorting rule of simplified Chinese ,sensotivityIs sensitivity , Includebase、accent、case、variantThese sensitivities
example 1- Pure array
var array = [' wuhan ', ' Beijing ', ' Shanghai ', ' tianjin '];
var resultArray = array.sort(
function compareFunction(param1, param2) {
return param1.localeCompare(param2,"zh");
}
);
console.log(resultArray);
example 2- Array objects
// Data to sort
let data = [
{
chinese: ' zeiss ', english: 'Chase'},
{
chinese: ' Allen ', english: 'Allen'},
{
chinese: ' Zola ', english: 'Zola'},
{
chinese: ' Baker, ', english: 'Baker'},
{
chinese: ' Berg ', english: 'Berg'},
{
chinese: ' Fitch ', english: 'Fitch'},
{
chinese: ' Dean ', english: 'Dean'},
{
chinese: ' Earl ', english: 'Earle'},
{
chinese: ' Henry ', english: 'Henry'},
]
// Sort according to the first letter of Chinese characters
// Use the arrow function
//【 notes 】localeCompare() yes js Built-in methods
data.sort((a, b)=> b.chinese.localeCompare(a.chinese, 'zh')); //z~a Sort
data.sort((a, b)=> a.chinese.localeCompare(b.chinese, 'zh')); //a~z Sort
console.log(data);
// Sort according to English Compare The first letter ASCLL code
console.log(data[0].english.charCodeAt(0));
ata.sort((a, b) => b.english.charCodeAt(0) - a.english.charCodeAt(0)); //z~a Sort
data.sort((a, b) => a.english.charCodeAt(0) - b.english.charCodeAt(0)); //a~z Sort
console.log(data);
attach :localeCompare() Method
js Provides a string comparison method localeCompare(), This method returns a number to indicate that a reference string and a comparison string are sorted first , After or the same . This method is basically not used alone , Most of the time it is used with string sorting .
grammar
stringObject.localeCompare(target)
Return value
Figures that illustrate the results of the comparison .
- If stringObject Less than target, be localeCompare() Back to less than 0 Number of numbers .
- If stringObject Greater than target, The method returns greater than 0 Number of numbers .
- If two strings are equal , Or according to the local collation , This method returns 0.
Method call
1、 Separate calls to methods : Simply compare two strings , Just check the return value .
var str = 'aaa',
strCom = 'bbb',
strCom2 = 'aaa';
str.localeCompare(strCom); //-1
strCom.localeCompare(str); //1
str.localeCompare(strCom2); //0
2、 With the call of sorting : This method is rarely used to compare strings alone , In most cases, it is used in conjunction with the sorting of strings .
ar strList = ['cc', 'ee', 'ca', 'aa'];
strList.sort((a, b) => {
return a.localeCompare(b);
});
console.log(strList); //["aa", "ca", "cc", "ee"]
explain
hold < and > When the operator is applied to a string , They only use characters Unicode Encoding comparison string , Regardless of local sorting rules . The order generated in this way is not necessarily correct . for example , In Spanish , Which character “ch” Usually appears as a letter “c” and “d” Sort by characters between .
localeCompare() Method to compare strings , Consider the default local collation .ECMAscript The standard does not specify how to make local specific comparisons , It only stipulates that the function adopts the sorting rules provided by the underlying operating system .
from z~a
// Sort according to the first letter of Chinese characters
// Use the arrow function
//【 notes 】localeCompare() yes js Built-in methods
data.sort((a, b)=> b.chinese.localeCompare(a.chinese, 'zh')); //z~a Sort
from a~z
data.sort((a, b)=> a.chinese.localeCompare(b.chinese, 'zh')); //a~z Sort
According to the English initials ASCLL Code to sort
from z~a
// Sort according to English Compare The first letter ASCLL code
// Use the arrow function
data.sort((a, b) => b.english.charCodeAt(0) - a.english.charCodeAt(0)); //z~a Sort
from a~z
data.sort((a, b) => a.english.charCodeAt(0) - b.english.charCodeAt(0)); //a~z Sort
边栏推荐
- Is it profitable to host an Olympic Games?
- No Yum source to install SPuG monitoring
- ICML 2022 | flowformer: task generic linear complexity transformer
- 新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- Data Lake (VIII): Iceberg data storage format
- Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
- [MySQL] basic use of cursor
- 3D人脸重建:从基础知识到识别/重建方法!
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- 请问sql group by 语句问题
猜你喜欢

C language operators

数据湖(八):Iceberg数据存储格式

15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace

Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?

防火墙基础之外网服务器区部署和双机热备

Introduction to the use of SAP Fiori application index tool and SAP Fiori tools

ICML 2022 | flowformer: task generic linear complexity transformer
![[wechat applet] operation mechanism and update mechanism](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
[wechat applet] operation mechanism and update mechanism

2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history

Interviewer: what is the internal implementation of ordered collection in redis?
随机推荐
js中,字符串和数组互转(二)——数组转为字符串的方法
华为设备命令
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
爱可可AI前沿推介(7.6)
请问sql group by 语句问题
Web开发小妙招:巧用ThreadLocal规避层层传值
What is the problem with the SQL group by statement
Nodejs教程之Expressjs一篇文章快速入门
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
Forward maximum matching method
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
js中,字符串和数组互转(一)——字符串转为数组的方法
如何实现常见框架
PG basics -- Logical Structure Management (transaction)
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
What is the difference between procedural SQL and C language in defining variables
面试官:Redis中有序集合的内部实现方式是什么?