Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-comparer-builder

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

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`

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
decreased by-42.86%
Maintainers
1
Weekly downloads
 
Created
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

Package last updated on 26 Mar 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc