Giter Club home page Giter Club logo

Comments (1)

lionel-rowe avatar lionel-rowe commented on June 21, 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']

from it-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.