2.4.14 • Published 9 months ago

@thi.ng/compare v2.4.14

Weekly downloads
3,047
License
Apache-2.0
Repository
github
Last release
9 months ago

compare

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

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

Since v1.2.0 additional higher-order comparators are included, e.g. to reverse the ordering of an existing comparator and allow hierarchical sorting by multiple keys/dimensions, each with their own optional comparator. See examples below.

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/compare

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/compare"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

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

Package sizes (gzipped, pre-treeshake): ESM: 396 bytes

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Color palette generation via dominant color extraction from uploaded imagesDemoSource
Full umbrella repo doc string search w/ paginated resultsDemoSource
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)
[...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)
[...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)
[...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`
[...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

Karsten Schmidt

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 - 2021 Karsten Schmidt // Apache Software License 2.0

2.4.14

9 months ago

2.4.13

10 months ago

2.4.10

11 months ago

2.4.12

10 months ago

2.4.11

10 months ago

2.4.9

11 months ago

2.4.8

11 months ago

2.4.7

11 months ago

2.4.6

12 months ago

2.4.5

1 year ago

2.4.4

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.4.3

1 year ago

2.4.2

1 year ago

2.3.6

1 year ago

2.3.8

1 year ago

2.3.7

1 year ago

2.3.9

1 year ago

2.3.10

1 year ago

2.3.5

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.0

2 years ago

2.2.27

2 years ago

2.2.26

2 years ago

2.2.25

2 years ago

2.2.24

2 years ago

2.2.23

2 years ago

2.2.22

2 years ago

2.2.21

2 years ago

2.2.20

2 years ago

2.2.19

2 years ago

2.2.17

2 years ago

2.2.18

2 years ago

2.2.15

2 years ago

2.2.16

2 years ago

2.2.14

2 years ago

2.2.13

2 years ago

2.2.11

2 years ago

2.2.12

2 years ago

2.2.10

2 years ago

2.2.9

2 years ago

2.2.8

2 years ago

2.2.7

2 years ago

2.2.6

2 years ago

2.1.38

2 years ago

2.1.36

2 years ago

2.1.37

2 years ago

2.1.34

2 years ago

2.1.35

2 years ago

2.1.33

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.1.32

2 years ago

2.1.31

3 years ago

2.1.30

3 years ago

2.1.27

3 years ago

2.1.28

3 years ago

2.1.29

3 years ago

2.1.26

3 years ago

2.1.25

3 years ago

2.1.23

3 years ago

2.1.24

3 years ago

2.1.19

3 years ago

2.1.21

3 years ago

2.1.22

3 years ago

2.1.20

3 years ago

2.1.16

3 years ago

2.1.17

3 years ago

2.1.14

3 years ago

2.1.15

3 years ago

2.1.13

3 years ago

2.1.18

3 years ago

2.1.12

3 years ago

2.1.11

3 years ago

2.1.9

3 years ago

2.1.10

3 years ago

2.1.8

3 years ago

2.1.6

4 years ago

2.1.7

3 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.0.8

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.3

4 years ago

2.1.0

4 years ago

2.0.7

4 years ago

2.0.4

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.34

4 years ago

1.3.32

4 years ago

1.3.33

4 years ago

1.3.31

4 years ago

1.3.30

4 years ago

1.3.29

4 years ago

1.3.28

5 years ago

1.3.24

5 years ago

1.3.25

5 years ago

1.3.26

5 years ago

1.3.27

5 years ago

1.3.23

5 years ago

1.3.22

5 years ago

1.3.21

5 years ago

1.3.20

5 years ago

1.3.19

5 years ago

1.3.18

5 years ago

1.3.17

5 years ago

1.3.16

5 years ago

1.3.15

5 years ago

1.3.14

5 years ago

1.3.13

5 years ago

1.3.11

5 years ago

1.3.12

5 years ago

1.3.10

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.12

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago