Search
⌘K

Array Sort

Utilities for sorting arrays.

Example Usage

import {
  sortArrayByDate,
  sortArrayByNumber,
  sortArrayByString,
} from './array-sort';

// sortArrayByDate - asc
const input = [
  { name: 'B', date: new Date('2022-02-01') },
  { name: 'A', date: new Date('2022-01-01') },
  { name: 'C', date: new Date('2022-03-01') },
];
const sortedAsc = input
  .toSorted((a, b) => sortArrayByDate(a.date, b.date, 'asc'))
  .map(item => item.name);
// ⏬
['A', 'C', 'B']

// sortArrayByDate - desc
const input = [
  { name: 'B', date: new Date('2022-02-01') },
  { name: 'A', date: new Date('2022-01-01') },
  { name: 'C', date: new Date('2022-03-01') },
];
const sortedDesc = input
  .toSorted((a, b) => sortArrayByDate(a.date, b.date, 'desc'))
  .map(item => item.name);
// ⏬
['C', 'B', 'A']

// sortArrayByNumber - asc
const input = [3, 1, 2];
const sortedAsc = input.toSorted((a, b) => sortArrayByNumber(a, b, 'asc'));
// ⏬
[1, 2, 3]

// sortArrayByNumber - desc
const input = [3, 1, 2];
const sortedDesc = input.toSorted((a, b) => sortArrayByNumber(a, b, 'desc'));
// ⏬
[3, 2, 1]

// sortArrayByString - asc
const input = ['B', 'A', 'C'];
const sortedAsc = input.toSorted((a, b) => sortArrayByString(a, b, 'asc'));
// ⏬
['A', 'B', 'C']

// sortArrayByString - desc
const input = ['B', 'A', 'C'];
const sortedDesc = input.toSorted((a, b) => sortArrayByString(a, b, 'desc'));
// ⏬
['C', 'B', 'A']

Dependencies

No dependencies

Auto Install

npx shadcn@latest add https://shadcn-registry-ts.vercel.app/r/util-array-sort.json

Manual Install

array-sort.ts
/** Array.sort() function - used to sort array by date */
export const sortArrayByDate = (a: Date, b: Date, direction: 'asc' | 'desc') => {
  // util
  const normalizeDate = (d: Date) => new Date(new Date(d).setHours(0, 0, 0, 0)).valueOf();
  // logic
  if (direction === 'asc') {
    return normalizeDate(a) - normalizeDate(b);
  }
  return normalizeDate(b) - normalizeDate(a);
};

/** Array.sort() function - used to sort array by number */
export const sortArrayByNumber = (a: number, b: number, direction: 'asc' | 'desc') => {
  if (direction === 'asc') {
    return a - b;
  }
  return b - a;
};

/** Array.sort() function - used to sort array by string */
export const sortArrayByString = (a: string, b: string, direction: 'asc' | 'desc') => {
  if (direction === 'asc') {
    return a.localeCompare(b);
  }
  return b.localeCompare(a);
};

Test

array-sort.test.ts
import { describe, it, expect } from 'vitest';

import {
  sortArrayByDate,
  sortArrayByNumber,
  sortArrayByString,
} from './array-sort';

describe('array - sortArrayByDate', () => {

  it('do it - asc', () => {
    const input = [
      { name: 'B', date: new Date('2022-02-01') },
      { name: 'A', date: new Date('2022-01-01') },
      { name: 'C', date: new Date('2022-03-01') },
    ];
    const sortedAsc = input.toSorted((a, b) => sortArrayByDate(a.date, b.date, 'asc'));
    expect(sortedAsc.map(item => item.name)).toEqual(['A', 'B', 'C']);
  });
  it('do it - desc', () => {
    const input = [
      { name: 'B', date: new Date('2022-02-01') },
      { name: 'A', date: new Date('2022-01-01') },
      { name: 'C', date: new Date('2022-03-01') },
    ];
    const sortedAsc = input.toSorted((a, b) => sortArrayByDate(a.date, b.date, 'desc'));
    expect(sortedAsc.map(item => item.name)).toEqual(['C', 'B', 'A']);
  });

});

describe('array - sortArrayByNumber', () => {

  it('do it - asc', () => {
    const input = [3, 1, 2];
    const sortedAsc = input.toSorted((a, b) => sortArrayByNumber(a, b, 'asc'));
    expect(sortedAsc).toEqual([1, 2, 3]);
  });
  it('do it - desc', () => {
    const input = [3, 1, 2];
    const sortedAsc = input.toSorted((a, b) => sortArrayByNumber(a, b, 'desc'));
    expect(sortedAsc).toEqual([3, 2, 1]);
  });

});

describe('array - sortArrayByString', () => {

  it('do it - asc', () => {
    const input = ['B', 'A', 'C'];
    const sortedAsc = input.toSorted((a, b) => sortArrayByString(a, b, 'asc'));
    expect(sortedAsc).toEqual(['A', 'B', 'C']);
  });
  it('do it - desc', () => {
    const input = ['B', 'A', 'C'];
    const sortedAsc = input.toSorted((a, b) => sortArrayByString(a, b, 'desc'));
    expect(sortedAsc).toEqual(['C', 'B', 'A']);
  });

});

Command Palette

Search for a command to run...