Multi-level sorting with Array.sort using ts-comparer-builder
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://github.com/alexjoverm/typescript-library-starter.