sortutil

package
v12.48.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: Apache-2.0 Imports: 3 Imported by: 5

Documentation

Overview

Package sortutil provides methods for sorting slices

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NaturalLess

func NaturalLess(s1, s2 string) bool

NaturalLess compares two strings using natural ordering. This means that e.g. "abc2" < "abc12" This code based on sortorder package created by @fvbommel

func Strings

func Strings(s []string, caseInsensitive bool)

Strings sorts strings slice and support case insensitive mode

Example
stringSlice := []string{
	"Alisa",
	"Luna",
	"remedios",
	"Ona",
	"Eugene",
	"lorriane",
	"Zachariah",
	"cecily",
	"eleonora",
	"Dotty",
}

// Case insensitive sorting
Strings(stringSlice, false)

fmt.Println(stringSlice)

// Case sensitive sorting
Strings(stringSlice, true)

fmt.Println(stringSlice)
Output:

[Alisa Dotty Eugene Luna Ona Zachariah cecily eleonora lorriane remedios]
[Alisa cecily Dotty eleonora Eugene lorriane Luna Ona remedios Zachariah]

func StringsNatural

func StringsNatural(a []string)

StringsNatural sorts a slice of strings in natural order Limitation: only ASCII digits (0-9) are considered.

func VersionCompare

func VersionCompare(v1, v2 string) bool

VersionCompare compares 2 versions and returns true if v1 less v2. This function can be used for version sorting with structs

func Versions

func Versions(s []string)

Versions sorts versions slice

Example
versionSlice := []string{
	"2.0-5",
	"1.3b",
	"1.1",
	"1.3",
	"1.1.6",
	"1",
	"2.0",
	"2.0-1",
}

Versions(versionSlice)

fmt.Println(versionSlice)
Output:

[1 1.1 1.1.6 1.3 1.3b 2.0 2.0-1 2.0-5]

Types

This section is empty.

Jump to

Keyboard shortcuts

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