当前位置:网站首页>String sort
String sort
2022-07-24 09:41:00 【Dream height 1.8 meters】
1 array.sort()
Array comes with sort, Sort according to string rules
console.log([5,4,3,2,11].sort()) // 11,2,3,4,5
console.log(["5","4","3","2","11"].sort()) // 11,2,3,4,5
console.log(["5 5、 ... and ","4 Four ","3 3、 ... and ","2 Two ","11 One "].sort()) // 11 One ,2 Two ,3 3、 ... and ,4 Four ,5 5、 ... and
console.log([" 5、 ... and "," Four "," 3、 ... and "," Two "," One "].sort())// One , Two , 3、 ... and , Four , 5、 ... and
2 All numeric sort
console.log([5,4,3,2,11].sort((a, b) => a-b)) // 2,3,4,5,11
console.log(["5","4","3","2","11"].sort((a, b) => a-b)) // 2,3,4,5,11
console.log(["5 5、 ... and ","4 Four ","3 3、 ... and ","2 Two ","11 One "].sort((a, b) => a-b)) // 5 5、 ... and ,4 Four ,3 3、 ... and ,2 Two ,11 One
console.log([" 5、 ... and "," Four "," 3、 ... and "," Two "," One "].sort((a, b) => a-b))// 5、 ... and , Four , 3、 ... and , Two , One
3 Sort according to the number in the string
such as 1 class ,2 class ,……
const arr = ['5 class ', '4 class ', '3 class ', '2 class ', '11 class ']
// With numbers in front , Non numeric after
console.log(arr.sort((a, b) => parseFloat(a) - parseFloat(b))) // 2 class ,3 class ,4 class ,5 class ,11 class
// A string containing only one integer value , Use regular expressions directly to delete the characters of numbers
console.log(arr.sort((a, b) => a.replace(/[^\d]/g,' ') - b.replace(/[^\d]/g,' ')))
边栏推荐
- Firewalld firewall related commands
- [don't bother with intensive learning] video notes (III) 1. What is SARS?
- Vector control of permanent magnet synchronous motor (I) -- mathematical model
- Synchronized scope "concurrent programming"
- IdentityServer4入门
- Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter
- Basic knowledge of PHP - complete collection of PHP functions
- [example] v-contextmenu right click menu component
- 【笔记】什么是内核/用户空间 从CPU如何运行程序讲起
- [Luogu p5829] [template] mismatch tree (string) (KMP)
猜你喜欢

Write a simple memo using localstorage
![[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)](/img/31/57217a67533d8d37bf86d507996ed7.png)
[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)
![[example] v-contextmenu right click menu component](/img/d7/9287b24a6d9ada01a7f258dd34f0f8.jpg)
[example] v-contextmenu right click menu component

Lung CT segmentation challenge 2017 dataset download and description

Spark Learning: using RDD API to implement inverted index

Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter

Open source summer interview | learn with problems, Apache dolphin scheduler, Wang Fuzheng

Data center: started in Alibaba and started in Daas
![[don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking](/img/a8/0d3bd3cc2b3e1d43e201e5dfe4b729.png)
[don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking
![[don't bother to strengthen learning] video notes (IV) 1. What is dqn?](/img/74/51219a19595f93e7a85449f54d354d.png)
[don't bother to strengthen learning] video notes (IV) 1. What is dqn?
随机推荐
CAS principle [concurrent programming]
[don't bother to strengthen learning] video notes (IV) 1. What is dqn?
[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)
web安全入门-开源防火墙Pfsense安装配置
Scarcity in Web3: how to become a winner in a decentralized world
Account 1-3
Data center: started in Alibaba and started in Daas
The difference between & &, | and |
Understanding of magnetic parameters in Hall sensors
Open source summer interview | learn with problems, Apache dolphin scheduler, Wang Fuzheng
Write a simple memo using localstorage
PHP Basics - PHP magic constants
What's the difference between testing / developing programmers' professionalism and salted fish? They don't want to be excellent coders?
[don't bother with intensive learning] video notes (III) 1. What is SARS?
Aruba学习笔记06-无线控制AC基础配置(CLI)
MySQL Basics (I) -- SQL Basics
Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K
Lung CT segmentation challenge 2017 dataset download and description
Account 1-2
Vim: extend the semantic analysis function of YCM for the third-party library of C language