https://github.com/perliedman/trivial-compare
Simplest possible JavaScript comparison function, as a module
https://github.com/perliedman/trivial-compare
Last synced: over 1 year ago
JSON representation
Simplest possible JavaScript comparison function, as a module
- Host: GitHub
- URL: https://github.com/perliedman/trivial-compare
- Owner: perliedman
- Created: 2021-09-29T13:05:30.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-29T13:22:05.000Z (almost 5 years ago)
- Last Synced: 2025-03-18T15:08:27.858Z (over 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
trivial-compare
===============
The simplest possible compare function that can be used with JavaScript's `Array.prototype.sort`
and any data type that gives reasonable results for the `<` and `>` comparison operators.
In practice, this means you can use this library to sort numbers as well as strings without caring
what the elements actually are.
I find myself dropping this tiny utility function in more or less any app I write, so now there's
a stupid npm module to stop this madness.
(As an aside, it feels weird such a function would not be part of the standard library - I sort of
hope this is a misunderstanding on my part and this library is just totally redundant, please drop
me a note if there's a better way to do this!)
## API
There's just a single function exported by this library:
`compare(a, b)`
`comapare` will return `-1` if `a < b`, `1` if `a > b` and `0` otherwise (if they are equal, in other words).