numeralsort

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 2 Imported by: 0

README

numeralsort

Sort text strings so that text strings containing decimal numbers sort as expected by humans.

For example, the values

"file1.ext" "file10.ext" "file100.ext" "file2.ext" "file20.ext" "file3.ext"

sort to

"file1.ext" "file2.ext" "file3.ext" "file10.ext" "file20.ext" "file100.ext"

This is useful when filenames contain embedded numbers without '0' prefixes which would make a regular text sort come out in the desired order.

Documentation

Overview

Numeral-aware text sorting.

People (and log-rotation scripts) tend to number files without leading 0's. That results in filenames like "file1","file2",...,"file10",..., which when sorted the regular way puts "file10" between "file1" and "file2", rather than after "file9" where we think of it being.

This sort gets the human order:

"file1" < "file01" < "file2" < "file10"

I wrote this same algorithm years ago in 68000 assembler for the custom open-file dialog on an Amiga computer. It's still useful now, and will be as long as people name files.

Copyright 2016 Nicolas S. Dade

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Less

func Less(a, b string) bool

Less returns true if x < y in a numeral-aware comparison. It is suitable for use with Go's standard sort.Interface.

func Strings

func Strings(strs []string)

Strings is a utility function to sort a slice of strings using numeral-aware sort order. The name matched the name of the equivalent function in the standard sort package.

Types

type StringSlice

type StringSlice []string

StringSlice is a slice of strings sortable in numeral-aware order It implements sort.Interface. The name matches the equivalent type in the standard sort package.

func (StringSlice) Len

func (s StringSlice) Len() int

func (StringSlice) Less

func (s StringSlice) Less(i, j int) bool

func (StringSlice) Swap

func (s StringSlice) Swap(i, j int)

Jump to

Keyboard shortcuts

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