Comments (1)
Other than the obvious ascending-vs-descending, check Intl.Collator
options for some possible options (locale
could be another option). Also consider supporting a "naive" by-code-unit versions.
export type SortOptions = { dir: Dir } & (NaiveSortOptions | CollateSortOptions)
type Dir = 'asc' | 'desc'
type NaiveSortOptions = {
kind: 'naive'
}
type CollateSortOptions = {
kind: 'collate'
locale: string | Intl.Locale
} & Intl.CollatorOptions
export function sort(list: readonly string[], options: SortOptions) {
const sorted = options.kind === 'naive' ? naiveSort(list) : collateSort(list, options)
return options.dir === 'desc' ? sorted.reverse() : sorted
}
function naiveSort(list: readonly string[]) {
return [...list].sort((a, b) => a > b ? 1 : a < b ? -1 : 0)
}
function collateSort(list: readonly string[], { locale, ...options }: CollateSortOptions) {
return [...list].sort((a, b) => a.localeCompare(b, locale, options))
}
/* Usage */
const strings = ['a', 'A', 'b', 'B', 'á', '1', '2', '10', '一', '阿']
sort(strings, { kind: 'naive', dir: 'asc' })
// ['1', '10', '2', 'A', 'B', 'a', 'b', 'á', '一', '阿']
sort(strings, { kind: 'naive', dir: 'desc' })
// ['阿', '一', 'á', 'b', 'a', 'B', 'A', '2', '10', '1']
sort(strings, { kind: 'collate', dir: 'asc', locale: 'en-US' })
// ['1', '10', '2', 'a', 'A', 'á', 'b', 'B', '一', '阿']
sort(strings, { kind: 'collate', dir: 'asc', locale: 'en-US', numeric: true, caseFirst: 'upper' })
// ['1', '2', '10', 'A', 'a', 'á', 'B', 'b', '一', '阿']
sort(strings, { kind: 'collate', dir: 'asc', locale: 'zh-CN', numeric: true })
// ['1', '2', '10', '阿', '一', 'á', 'a', 'A', 'b', 'B']
from it-tools.
Related Issues (20)
- BCrypt
- Can't run docker on OpenShift
- SQL Schema 转 JAVA Bean
- 可以新增个工作日计算器吗
- msgpack to json
- Longer JSON will cause the styles to stretch out indefinitely, making the results difficult to see HOT 1
- Maybe add a tool for vmess to clash
- Kelvin temperature can't be negative
- Add example code in JSON to CSV
- [NEW TOOL] Mac Address Converter HOT 1
- Add pasting feature when need to upload file in base64 file converter
- Token Generator text box autosize not working
- SM2, SM3, and SM4
- Make the Docker Run to Docker-Compose bidirectional (or add Docker-Compose to Docker Run tool) HOT 2
- Byte conversion tool
- Time conversion
- Build faild HOT 1
- 是否可以支持 json 转 java 实体
- Unescape JSON Unicode sequences HOT 1
- Emoji: search function lags
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from it-tools.