Skip to content

biggyspender/ts-comparer-builder

Repository files navigation

Multi-level sort with ts-comparer-builder

ts-comparer-builder is an easy way of creating multi-level comparer functions that can be used in Array.sort

Example

Given a (very simple) Date type

export interface Date {
  day: number
  month: number
  year: number
}

and some unordered instances of it

const dates: Date[] = [
    { day: 1, month: 10, year: 2000 },
    { day: 1, month: 1, year: 2000 },
    { day: 2, month: 1, year: 2000 },
    { day: 1, month: 1, year: 1999 },
    { day: 1, month: 1, year: 2000 }
]

we can build a comparer function for it

const dateComparer = comparerBuilder<Date>()
    .sortKey(x => x.year)
    .thenKey(x => x.month)
    .thenKey(x => x.day)
    .build()

and sort our list of Date

dates.sort(dateComparer)

so that it looks like this:

[ { day: 1, month: 1, year: 1999 },
  { day: 1, month: 1, year: 2000 },
  { day: 1, month: 1, year: 2000 },
  { day: 2, month: 1, year: 2000 },
  { day: 1, month: 10, year: 2000 } ]

acknowledgements

Created using the wonderful https:/alexjoverm/typescript-library-starter.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published