You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@thi.ng/compare

Package Overview
Dependencies
Maintainers
1
Versions
177
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/compare

Comparators with support for types implementing the @thi.ng/api/ICompare interface

2.4.22
latest
Source
npmnpm
Version published
Weekly downloads
165K
-4.47%
Maintainers
1
Weekly downloads
 
Created
Source

@thi.ng/compare

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 209 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! ❤️

About

Comparators with optional support for types implementing the @thi.ng/api ICompare interface.

Generic comparison

Additional comparators

Operators

Status

STABLE - used in production

Search or submit any issues for this package

Installation

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>

JSDelivr documentation

For Node.js REPL:

const cmp = await import("@thi.ng/compare");

Package sizes (brotli'd, pre-treeshake): ESM: 804 bytes

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

Usage examples

Five projects in this repo's /examples directory are using this package:

ScreenshotDescriptionLive demoSource
Color palette generation via dominant color extraction from uploaded imagesDemoSource
Piechart visualization of CSV dataDemoSource
Full umbrella repo doc string search w/ paginated resultsDemoSource
Tree-based UI to find & explore thi.ng projects via their associated keywordsDemoSource
Triple store query results & sortable tableDemoSource

API

Generated API docs

ICompare support

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

Cluster sort w/ multiple sort keys

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 }
// ]

Authors

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
}

License

© 2016 - 2025 Karsten Schmidt // Apache License 2.0

Keywords

comparator

FAQs

Package last updated on 10 Jul 2025

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