naturally

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 4 Imported by: 0

README

CI Go Reference

naturally

Go implementation of a natural sort. Numbers and non-numbers are treated as separate sub-parts to make the sort more natural for human readability. Inspired by the Perl implementation Sort::Naturally.

Install

go get github.com/dangogh/naturally

Usage

files := []string{"photo10.jpg", "photo9.jpg", "photo1.jpg"}

naturally.Sort(files)
// [photo1.jpg photo9.jpg photo10.jpg]
// a plain sort.Strings would give [photo1.jpg photo10.jpg photo9.jpg]

naturally.SortCI(files) // same, ignoring case

For use with sort.Sort / sort.Stable directly, the StringSlice and CIStringSlice types implement sort.Interface:

sort.Stable(naturally.StringSlice(files))
sort.Stable(naturally.CIStringSlice(files))

Ordering rules

  • Numeric segments compare by value, with no digit limit, so "99999999999999999999" sorts before "100000000000000000000".
  • Equal values with different widths order by the shorter run first: "A1" < "A01" < "A001".
  • A string containing a digit sorts before one that does not, so "b2" < "Banana". The alphabetic prefixes are not compared when only one side has digits.
  • Segments of non-ASCII digits (for example Arabic-Indic ٣) are compared as text rather than by value.
  • SortCI uses simple Unicode case folding. Values that differ only by case compare equal, so use sort.Stable if you need their relative order preserved.

Documentation

Overview

Package naturally implements "natural" sort ordering: the alphabetic portions of a string are compared alphabetically and the numeric portions are compared numerically, so that "A2" sorts before "A11".

Ordering details worth knowing:

  • Numeric segments are compared by value, with no limit on the number of digits, so "99999999999999999999" sorts before "100000000000000000000".
  • When two numeric segments have the same value but different lengths, the shorter (less zero-padded) one sorts first: "A1" < "A01" < "A001".
  • A string containing a digit sorts before one that does not, so "b2" < "Banana". See the package README for the rationale.
  • Segments of non-ASCII digits (for example Arabic-Indic "٣") are compared as text rather than by value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort added in v0.2.0

func Sort(s []string)

Sort sorts a string slice using natural ordering (case-sensitive).

func SortCI added in v0.2.0

func SortCI(s []string)

SortCI sorts a string slice using natural ordering (case-insensitive).

Types

type CIStringSlice added in v0.2.0

type CIStringSlice sort.StringSlice

CIStringSlice attaches the methods of sort.Interface to []string, sorting in natural order while ignoring case.

func (CIStringSlice) Len added in v0.2.0

func (p CIStringSlice) Len() int

Len is the number of elements in the collection.

func (CIStringSlice) Less added in v0.2.0

func (p CIStringSlice) Less(a, b int) bool

Less reports whether element a should sort before element b, ignoring case.

func (CIStringSlice) Swap added in v0.2.0

func (p CIStringSlice) Swap(a, b int)

Swap swaps the elements with indexes a and b.

type StringSlice

type StringSlice sort.StringSlice

StringSlice attaches the methods of sort.Interface to []string, sorting in natural order (case-sensitive).

func (StringSlice) Len

func (p StringSlice) Len() int

Len is the number of elements in the collection.

func (StringSlice) Less

func (p StringSlice) Less(a, b int) bool

Less reports whether element a should sort before element b.

func (StringSlice) Swap

func (p StringSlice) Swap(a, b int)

Swap swaps the elements with indexes a and b.

Jump to

Keyboard shortcuts

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