
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@thi.ng/compare
Advanced tools
Comparators with support for types implementing the @thi.ng/api/ICompare interface
[!NOTE] This is one of 210 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
Comparators with optional support for types implementing the @thi.ng/api
ICompare
interface.
compareByKey()compareByKeys2()compareByKeys3()compareByKeys4()compareLengthAsc()compareLengthDesc()compareLex()compareNumAsc()compareNumDesc()reverse()STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/compare
ESM import:
import * as cmp from "@thi.ng/compare";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/compare"></script>
For Node.js REPL:
const cmp = await import("@thi.ng/compare");
Package sizes (brotli'd, pre-treeshake): ESM: 804 bytes
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
Five projects in this repo's /examples directory are using this package:
| Screenshot | Description | Live demo | Source |
|---|---|---|---|
![]() | Color palette generation via dominant color extraction from uploaded images | Demo | Source |
![]() | Piechart visualization of CSV data | Demo | Source |
| Full umbrella repo doc string search w/ paginated results | Demo | Source | |
![]() | Tree-based UI to find & explore thi.ng projects via their associated keywords | Demo | Source |
![]() | Triple store query results & sortable table | Demo | Source |
import { ICompare } from "@thi.ng/api";
import { compare } from "@thi.ng/compare";
class Foo implements ICompare<Foo> {
x: number;
constructor(x: number) {
this.x = x;
}
compare(o: Foo) {
return compare(this.x, o.x);
}
}
compare(new Foo(1), new Foo(2));
// -1
Key-based object comparison is supported for 1 - 4 keys / dimensions.
import * as cmp from "@thi.ng/compare";
const src = [
{ id: "charlie", age: 66 },
{ id: "bart", age: 42 },
{ id: "alice", age: 23 },
{ id: "dora", age: 11 },
];
// cluster sort by id -> age (default comparators)
console.log(
[...src].sort(cmp.compareByKeys2("id", "age"))
);
// [
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 },
// { id: 'dora', age: 11 }
// ]
// cluster sort by age -> id (default comparators)
console.log(
[...src].sort(cmp.compareByKeys2("age", "id"))
);
// [
// { id: 'dora', age: 11 },
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 }
// ]
// cluster sort by age -> id
// (custom comparator for `age` key)
console.log(
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc))
);
// [
// { id: 'charlie', age: 66 },
// { id: 'bart', age: 42 },
// { id: 'alice', age: 23 },
// { id: 'dora', age: 11 }
// ]
// using `reverse()` comparator for `id`
console.log(
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)))
);
// [
// { id: 'dora', age: 11 },
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 }
// ]
If this project contributes to an academic publication, please cite it as:
@misc{thing-compare,
title = "@thi.ng/compare",
author = "Karsten Schmidt",
note = "https://thi.ng/compare",
year = 2016
}
© 2016 - 2025 Karsten Schmidt // Apache License 2.0
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including comparison operations. It offers functions like _.isEqual for deep equality checks and _.sortBy for sorting collections. Compared to @thi.ng/compare, Lodash is more comprehensive but also larger in size.
fast-deep-equal is a small and fast library for deep equality checks. It is highly optimized for performance and is often used when deep comparison of objects or arrays is needed. Unlike @thi.ng/compare, it focuses solely on deep equality and does not provide other comparison utilities.
Ramda is a functional programming library for JavaScript that includes a variety of utility functions, including comparison functions like R.equals and R.sort. It emphasizes immutability and functional programming principles. While it offers similar comparison functionalities, it is part of a broader functional programming toolkit.
FAQs
Comparators with support for types implementing the @thi.ng/api/ICompare interface
We found that @thi.ng/compare demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.