Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort list of strings alphabetically #1089

Open
3 tasks done
fincentxyz opened this issue May 14, 2024 · 1 comment
Open
3 tasks done

Sort list of strings alphabetically #1089

fincentxyz opened this issue May 14, 2024 · 1 comment
Labels
enhancement New feature or request triage

Comments

@fincentxyz
Copy link

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

  • Check the feature is not already implemented in the project.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • Check that the feature can be implemented in a client side only app (IT-Tools is client side only, no server).
@fincentxyz fincentxyz added enhancement New feature or request triage labels May 14, 2024
@lionel-rowe
Copy link

lionel-rowe commented May 15, 2024

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']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage
Projects
None yet
Development

No branches or pull requests

2 participants