stringsort

package module
v0.0.0-...-a495390 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package stringsort provides support code for sorting strings.

Mixed Keys

Ordinarily strings are sorted lexicographically by character. This is simple and consistent, but when applied to UI elements can be unintuitive for users. For example, lexicgraphically sorting a list of filenames will produce an order like

file-1.png
file-10.png
file-2.png

That is, "file 2" is listed after "file 10". One way to address this is to treat runs of digits differently in comparison: Instead of comparing them digit-by-digit, treat the entire run as a single value.

The MixedKey type supports this representation, representing a string as a sequence of "spans", each consisting of a non-digit string followed by an integer value corresponding to a digit string. Lexicographic comparison of these keys will preserve the intuitive ordering of digit sequences.

This approach emulates the ordering used by the macOS Finder for file names.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByMixedKey

func ByMixedKey(ss []string) sort.Interface

ByMixedKey returns a sorter that orders ss non-decreasing by mixed key. The keys are precomputed at the point of construction.

Note that non-identical strings may have equal mixed keys, consider for example "xyzzy1" and "xyzzy01". To ensure a deterministic order, ties on key order are broken using the lexicgraphic order of the original strings.

Types

type MixedKey

type MixedKey []nspan

A MixedKey is a lexicographic sort key for a string that partitions it into paired runs of non-digits and decimal digits. The runs of digits are interpreted as integer values for comparison.

For example, the string "alpha25bravo-3" generates the mixed key:

("alpha", 25) ("bravo-", 3)

while the string "101 dalmatians" generates the mixed key:

("", 101) (" dalmatians", 0)

func ParseMixed

func ParseMixed(s string) MixedKey

ParseMixed parses s into a MixedKey.

Jump to

Keyboard shortcuts

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