handysort

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

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

Go to latest
Published: Apr 21, 2015 License: MIT Imports: 1 Imported by: 3

README

❗ WARNING: 13x slower than https://github.com/fvbommel/util/tree/master/sortorder

Handysort Build Status GoDoc

This is a Go package implementing a correct comparison function to compare alphanumeric strings with respect to their integer parts.

For example, this is default result of strings sort:

hello1
hello10
hello11
hello2
hello3

This is handysort:

hello1
hello2
hello3
hello10
hello11

However, this is about 5x-8x times slower than the default sort version. (benchmarks available)

Usage

package main

import (
	"github.com/xlab/handysort"
	"sort"
)

func main() {
	s1, s2 := "hello2", "hello10"
	// instead of s1 < s2
	less := handysort.StringLess(s1, s2)

	s := []string{"hello5", "hello10", "hello1"}
	// instead of sort.Strings
	sort.Sort(handysort.Strings(s))
}

Benchmarking

$ go test -bench=.

Documentation

Overview

Package handysort implements an alphanumeric string comparison function in order to sort alphanumeric strings correctly.

Default sort (incorrect):

abc1
abc10
abc12
abc2

Handysort:

abc1
abc2
abc10
abc12

Please note, that handysort is about 5x-8x times slower than a simple sort, so use it wisely.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringLess

func StringLess(s1, s2 string) (less bool)

StringLess compares two alphanumeric strings correctly.

Types

type Strings

type Strings []string

Strings implements the sort interface, sorts an array of the alphanumeric strings in decreasing order.

func (Strings) Len

func (a Strings) Len() int

func (Strings) Less

func (a Strings) Less(i, j int) bool

func (Strings) Swap

func (a Strings) Swap(i, j int)

Jump to

Keyboard shortcuts

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