Help us shape the future of Go! Take the 2026 Go Developer Survey

bib

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 6 Imported by: 1

README

Bib

Go Reference Tests Coverage Status

This is bib, a library and command-line tool for bibliographic sorting of English-language strings.

Usage and installation

For library usage, please see the Go doc.

Command-line installation:

go install github.com/bobg/bib

Usage:

bib <INPUT >OUTPUT

Explanation

A bibliographic sort discards a leading article (“The,” “A,” and “An”), disregards case and some punctuation like apostrophes and hyphens, converts & to and, and spells out numbers.

In spelling out numbers, bib tries to distinguish between cardinals (“one,” “two,” “three,” etc) and ordinals (“first,” “second,” “third,” etc). It also tries to distinguish years from other kinds of number.

So a title like 42nd Street looks to bib like forty-second street; The 400 Blows looks like four hundred blows; 1917 looks like nineteen seventeen (not “one thousand nine hundred seventeen”), and so on.

Example

If file INPUT contains these lines, in this order:

The Gumball Rally
The 501st Legion
The 600th Floor
1917
42nd Street
350000000 Years of Solitude
It's Garry Shandling's Show
The 40-Year-Old Virgin
The 30th Floor
9 to 5

then running bib <INPUT will produce this output:

The 501st Legion
The 40-Year-Old Virgin
42nd Street
The Gumball Rally
It's Garry Shandling's Show
9 to 5
1917
The 600th Floor
The 30th Floor
350000000 Years of Solitude

Documentation

Overview

Package bib contains functions for (English-language) bibliographic sorting of strings.

A bibliographic sort is one that ignores a leading article ("the," "a," "an") and treats leading numbers as if they're spelled out. Characters other than letters and digits are ignored, except that "&" is converted to the spelled-out word "and," and hyphens are converted to spaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Key

func Key(s string) string

Key converts an input string to a bibliographic sort key. It does this by trimming whitespace, converting to lower case, removing leading articles, converting "&" to "and," and hyphens to spaces, converting leading numbers to their spelled-out equivalents (distinguishing between regular ordinals, cardinal numbers, and years), and finally removing all characters other than letters, digits, and whitespace.

func Less

func Less(a, b string) bool

Less tells whether a comes before b in a bibliograhic sort. It does this by calling Key on each string, qv.

func Sort

func Sort(strs []string)

Sort sorts the input slice bibliographically, in place. This is more time-efficient than calling sort.Slice with Less as the comparison function. That would call Key on each string in the slice more than once, on average. Instead, this function computes the keys for all the strings exactly once, then sorts the slice using those keys (using slices.KeyedSort).

Types

This section is empty.

Directories

Path Synopsis
cmd
bib command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL