Socket
Socket
Sign inDemoInstall

ts-comparer-builder

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-comparer-builder

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


Version published
Weekly downloads
48
increased by242.86%
Maintainers
1
Install size
25.5 kB
Created
Weekly downloads
 

Readme

Source

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://github.com/alexjoverm/typescript-library-starter.

FAQs

Last updated on 26 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc