natural

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2020 License: Apache-2.0 Imports: 1 Imported by: 124 modules, 152 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

natural

Yet another natural sort, with 100% test coverage and a benchmark. It allocates no memory, doesn't depend on package sort and hence doesn't depend on reflect.

GoDoc

Benchmarks

On Go 1.10.1.

On a Xeon:

$ go test -bench=. -benchmem -cpu 1
goos: linux
goarch: amd64
pkg: github.com/maruel/natural
BenchmarkNative                200000000       8.63 ns/op     0 B/op     0 allocs/op
BenchmarkLessStringOnly        100000000       18.7 ns/op     0 B/op     0 allocs/op
BenchmarkLessDigits             50000000       30.5 ns/op     0 B/op     0 allocs/op
BenchmarkLessStringDigits       50000000       31.3 ns/op     0 B/op     0 allocs/op
BenchmarkLessDigitsTwoGroups    20000000       64.4 ns/op     0 B/op     0 allocs/op

On a Raspberry Pi 3:

$ go test -bench=. -benchmem -cpu 1
goos: linux
goarch: arm
pkg: github.com/maruel/natural
BenchmarkNative                 10000000        148 ns/op     0 B/op      0 allocs/op
BenchmarkLessStringOnly          5000000        312 ns/op     0 B/op      0 allocs/op
BenchmarkLessDigits              2000000        656 ns/op     0 B/op      0 allocs/op
BenchmarkLessStringDigits        2000000        679 ns/op     0 B/op      0 allocs/op
BenchmarkLessDigitsTwoGroups     1000000       1480 ns/op     0 B/op      0 allocs/op

Coverage:

$ go test -cover
PASS
coverage: 100.0% of statements
ok     github.com/maruel/natural       0.012s

Documentation

Overview

Package natural defines a natural "less" to compare two strings while interpreting natural numbers.

Example
package main

import (
	"fmt"
	"sort"
	"strings"

	"github.com/maruel/natural"
)

func main() {
	items := []string{
		"gpio10",
		"gpio1",
		"gpio20",
	}
	sort.Sort(natural.StringSlice(items))
	fmt.Println(strings.Join(items, "\n"))
}
Output:
gpio1
gpio10
gpio20

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Less

func Less(a, b string) bool

Less does a 'natural' comparison on the two strings.

This is occasionally nicknamed 'natsort'.

It treats decimal numbers as value, so that Less("10", "2") return true.

Types

type StringSlice

type StringSlice []string

StringSlice attaches the methods of Interface to []string, sorting in increasing order using natural order.

func (StringSlice) Len

func (p StringSlice) Len() int

func (StringSlice) Less

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

func (StringSlice) Swap

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

Jump to

Keyboard shortcuts

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