当前位置:网站首页>行政路线编码 字母+数字的排序方式
行政路线编码 字母+数字的排序方式
2022-06-30 03:00:00 【不知言】
第一种:
// lxbm: G1,G4,SL3596,H89...
const array = []
array.map(item => {
const tmpArr = item.lxbm.split('')
const reg = /[a-z,A-Z]/g
item.sortLetter = '' // 字母转数字后的结果
item.sortNum = '' // 纯数字的结果
tmpArr.map(it => {
if (reg.test(it)) {
item.sortLetter += it.charCodeAt(0).toString() // 字母转换成数字
} else {
item.sortNum += it.toString()
}
})
return item
})
// sort排序
array.sort((a, b) => {
return this.sortLxbm(a, b)
})
// 排序规则
sortLxbm(a, b) {
// 字母一样时,按路线数字编号排序
if (Number(a.sortLetter) === Number(b.sortLetter)) {
return Number(a.sortNum) - Number(b.sortNum)
}
// 按字母排序
return Number(a.sortLetter) - Number(b.sortLetter)
}
第二种:
// lxbm: G1,G4,SL3596,H89...
// lxmc:京哈,京广,京藏...
const array = []
const nameArr = []
array.map(item => {
item.lxmcn = item.lxbm + '-' + item.lxmc
nameArr.push(item.lxmcn)
return item
})
nameArr.sort() // 用拼接好的名称直接sort排序,sort会自动处理字母和数字的顺序(或许最终结果不是你想要的,这时就需要自己定制化调整逻辑了...)
// 然后结果数组按特定的名称数组进行排序,参考如下:
/** * 数组按特定顺序排序 * 比如:已有数组按以下顺序排列:风雪、雨露、雷霆、暴雨、风沙、飞雾 * 核心就是利用自由的sort方法 * @param now 当前需要排序的数组 * @param mirror 指定的排序规则 */
let now = ['雨露','白霜','飞雾','暴雨','风雪','雷霆','风沙'];
let mirror = ['风雪','雨露','雷霆','暴雨','风沙','飞雾'];
let val = now.sort(function(a, b) {
// 对应不上的排到后面
if (mirror.indexOf(a) == -1 || mirror.indexOf(b) == -1) {
return mirror.indexOf(b) - mirror.indexOf(a);
} else {
return mirror.indexOf(a) - mirror.indexOf(b);
}
})
边栏推荐
猜你喜欢

怎么利用Redis实现点赞功能

What is the concept of string in PHP

Raki's notes on reading paper: named entity recognition as dependency parsing

IDEA 远程调试 Remote JVM Debug

Linear algebra Chapter 3 summary of vector and vector space knowledge points (Jeff's self perception)

约瑟夫环 数学解法

CMake教程系列-02-使用cmake代碼生成二進制

Unity timeline data binding

How to prevent duplicate submission under concurrent requests

中断操作:AbortController学习笔记
随机推荐
Prompt learning a blood case caused by a space
IDEA 远程调试 Remote JVM Debug
What is the concept of string in PHP
zabbix 触发器详解
Uniapp address translation latitude and longitude
mysql 主从数据库同步失败的原因
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
CMake教程系列-03-依赖管理
Azure developer news flash list of events of developers in June
Simulate activity startup mode in compose
多卡服务器使用
Raii memory management
Customize the buttons of jvxetable and the usage of $set under notes
CMake教程系列-02-使用cmake代码生成二进制
Lua Basics
Note the use of export/import and class inheritance in ES6
Use of Arthas
炒现货黄金的交易平台如何保障资金安全?
快速排序、聚簇索引、尋找數據中第k大的值
[oiclass] chess piece