it-tools
it-tools copied to clipboard
Sort list of strings alphabetically
What type of request is this?
New tool idea
Clear and concise description of the feature you are proposing
A text box in which the user inputs strings and with the click of a button they're sorted alphabetically and can be copied.
Is their example of this tool in the wild?
https://wordcounter.net/alphabetize https://alphabetizer.flap.tv/
Additional context
No response
Validations
- [X] Check the feature is not already implemented in the project.
- [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
- [X] Check that the feature can be implemented in a client side only app (IT-Tools is client side only, no server).
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']
Hi @fincentxyz, @lionel-rowe and @CorentinTh, implemented in #1302