Skip to content

Latest commit

 

History

History
64 lines (52 loc) · 963 Bytes

File metadata and controls

64 lines (52 loc) · 963 Bytes

Comparator

Implementation

type PredicateFunction<T> = (a: T, b: T) => boolean;

const comparator = <T>(predicate: PredicateFunction<T>) => {
  return (a: T, b: T) => {
    if (predicate(a, b)) return -1;
    if (predicate(b, a)) return 1;
    return 0;
  };
};

Usage

[5, 1, 2, 8].slice().sort(comparator((a, b) => a < b))

// [ 1, 2, 5, 8 ]

interface User {
  id: number;
  age: number;
}

const users: User[] = [
  {
    id: 1,
    age: 26,
  },
  {
    id: 2,
    age: 20,
  },
  {
    id: 3,
    age: 28,
  },
];

users.slice().sort(comparator((a, b) => a.age > b.age))

// [
//   {
//     "id": 3,
//     "age": 28
//   },
//   {
//     "id": 1,
//     "age": 26
//   },
//   {
//     "id": 2,
//     "age": 20
//   }
// ]

Resources