A JS class that formats name strings to semi-intelligently put the last name first, making them sortable by last name. Opinionated and English specific, but configurable.
Find a file
semantic-release-bot 5babbb81ea chore(release): 2.0.0 [skip ci]
## [2.0.0](https://github.com/ardouglass/sortable-names/compare/v1.0.0...v2.0.0) (2024-10-28)

### ⚠ BREAKING CHANGES

* may change the sorted order of items previously sorted with the compare helper.

### Bug Fixes

* compare sorting helper now behaves as expected ([5c3ced5](5c3ced5e11)), closes [#7](https://github.com/ardouglass/sortable-names/issues/7)
2024-10-28 15:38:06 +00:00
.github/workflows ci: remove CHANGELOG.md from preflight prettier check 2024-10-28 10:31:31 -05:00
.husky ci: add commitlint 2024-09-18 19:08:16 -05:00
demo fix!: compare sorting helper now behaves as expected 2024-10-28 10:37:24 -05:00
src fix!: compare sorting helper now behaves as expected 2024-10-28 10:37:24 -05:00
.gitignore feat: initial release 2024-09-18 11:54:57 -05:00
CHANGELOG.md chore(release): 2.0.0 [skip ci] 2024-10-28 15:38:06 +00:00
commitlint.config.js ci: add commitlint 2024-09-18 19:08:16 -05:00
eslint.config.js ci: ignore dist when linting 2024-09-18 12:47:52 -05:00
LICENSE Initial commit 2024-09-05 16:17:12 -05:00
lint-staged.config.js feat: initial release 2024-09-18 11:54:57 -05:00
package-lock.json ci: set up semantic-release tooling 2024-09-20 11:47:39 -05:00
package.json ci: set up semantic-release tooling 2024-09-20 11:47:39 -05:00
prettier.config.js feat: initial release 2024-09-18 11:54:57 -05:00
README.md feat: initial release 2024-09-18 11:54:57 -05:00
release.config.js ci: set up semantic-release tooling 2024-09-20 11:47:39 -05:00
tsconfig.json feat: initial release 2024-09-18 11:54:57 -05:00
tsconfig.types.json feat: initial release 2024-09-18 11:54:57 -05:00
vite.config.ts feat: initial release 2024-09-18 11:54:57 -05:00

sortable-names

A JS class that formats name strings to semi-intelligently put the last name first, making them sortable by last name. Opinionated and English specific, but configurable.

Why?

You might need something like this if you have a list of authors and want to make them sortable by last name. Moving the last part of the name isn't a full solution, because author names may have honorifics or suffixes that break a simple approach like that. A work could be authored by an organization as well, and rearranging the name wouldn't make sense.

What sortable-names does by default

  • Strips extra whitespace
  • Removes honorifics before names
  • Keeps suffixes like Jr, PhD, IV, etc. at the end of the name
  • Leaves organization names mostly alone, only moving articles to the end of the name

What you can configure

  • The articles to move to the end of organization names
  • The words that identify organizations
  • The honorifics to strip from the beginning of names
  • The suffixes to leave at the end of names

Installing

Add the dependency:

npm i sortable-names

Import it and use it somewhere:

import { SortableNames } from "sortable-names";

const sortableNames = new SortableNames();

const formattedName = sortableNames.getSortable(
  "Dr. Martin Luther King Jr., Ph.D."
);

console.log(formattedName);
// "King, Martin Luther, Jr, PhD"

Sort helper

If you have an array of formatted names, sorting them isn't entirely trivial. Things like accented letters and puncuation in names like O'Malley can result in undesirable sorting order. Use the provided helper method to fix this.

import { SortableNames } from "sortable-names";

const compare = SortableNames.compare;

const formattedNames = ["O'Malley, Virgil", "Oliver, John"];

// Compares the ' in O'Malley to the l in Oliver
console.log(formattedNames.toSorted());
// ["O'Malley, Virgil", "Oliver, John"]

// Ignores punctuation
console.log(formattedNames.toSorted(compare));
// ["Oliver, John", "O'Malley, Virgil"]

Examples using defaults

These examples are sorted using the sort helper.

Formatted Original
Arnim, Elizabeth von Elizabeth von Arnim
Bulwer-Lytton, Edward Edward Bulwer-Lytton
Burton, Richard Francis Sir Richard Francis Burton
Darío, Rubén Rubén Darío
Environmental Protection Agency, The The Environmental Protection Agency
Homer Homer
King, Martin Luther, Jr Martin Luther King Jr.
Ólafsdóttir, Auður Ava Auður Ava Ólafsdóttir
O'Mahony, Daniel Daniel O'Mahony
Parson, Annie-B Annie-B Parson
Shakespeare, William William Shakespeare
Shelley, Mary Wollstonecraft Mary Wollstonecraft Shelley
Smollett, T. T. Smollett
U.S. Government Accountability Office U.S. Government Accountability Office

Configuring options

If you are unhappy with the defaults or want to extend them, you can pass options into the constructor.

import { SortableNames } from "sortable-names";

/**
 * To extend an option, the defaults are
 * available as static properties
 */
const extendedOrgWords = [...SortableNames.defaultOrgWords, "Circus(?:es)"];

/**
 * Only support org names in Spanish
 */
const replacedArticles = ["El", "Las?", "Los?", "Una?"];

/**
 * Don't strip any honorifics
 */
const noHonorifics = [];

/**
 * Suffixes can also be extended or modified,
 * for example to support LinkedIn Lunatics like
 * Horatio Herbert Kitchener, KG, KP, GCB, OM, GCSI, GCMG, GCIE, PC
 */
const extendedNameSuffixes = [
  ...SortableNames.defaultNameSuffixes,
  "KG",
  "KP",
  "GCB",
  "OM",
  "GCSI",
  "GCMG",
  "GCIE",
  "PC",
];

const configuredSortableNames = new SortableNames({
  articles: replacedArticles,
  orgWords: extendedOrgWords,
  honorifics: noHonorifics,
  nameSuffixes: extendedNameSuffixes,
});

Happy sorting!