sorts: github.com/twotwotwo/sorts Index | Examples | Files | Directories

package sorts

import "github.com/twotwotwo/sorts"

Package sorts does parallel radix sorts of data by (u)int64, string, or []byte keys, and parallel quicksort. See the sorts/sortutil package for shortcuts for common slice types and help sorting floats.

Code:

package main

import (
    "fmt"
    "github.com/twotwotwo/sorts"
    "github.com/twotwotwo/sorts/sortutil"
)

type City struct {
    Name                string
    Latitude, Longitude float32
}

func (c City) String() string { return fmt.Sprintf("%s (%.1f, %.1f)", c.Name, c.Latitude, c.Longitude) }

// ByLatitude implements sort.Interface for []City based on
// the Latitude field, for sorting cities south to north.
type ByLatitude []City

func (a ByLatitude) Len() int      { return len(a) }
func (a ByLatitude) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// Float32Key and Float32Less make the sort handle the sign bit and sort NaN
// values to the end.  There are also Float64Key and Float64Less, and
// [Type]Key functions for int types.

// Key returns a uint64 that is lower for more southerly latitudes.
func (a ByLatitude) Key(i int) uint64 {
    return sortutil.Float32Key(a[i].Latitude)
}
func (a ByLatitude) Less(i, j int) bool {
    return sortutil.Float32Less(a[i].Latitude, a[j].Latitude)
}

func main() {
    cities := []City{
        {"Vancouver", 49.3, -123.1},
        {"Tokyo", 35.6, 139.7},
        {"Honolulu", 21.3, -157.8},
        {"Sydney", -33.9, 151.2},
    }

    fmt.Println(cities)
    sorts.ByUint64(ByLatitude(cities))
    fmt.Println(cities)

}

Code:

scores := []int{39, 492, 4912, 39, -10, 4, 92}
data := sortutil.IntSlice(scores)
data.Sort()
sorts.Flip(data) // high scores first
fmt.Println(scores)

Output:

[4912 492 92 39 39 4 -10]

Code:

groceries := []string{"peppers", "tortillas", "tomatoes", "cheese"}
sortutil.Strings(groceries) // or sortutil.Bytes([][]byte)
fmt.Println(groceries)

Output:

[cheese peppers tomatoes tortillas]

Index

Examples

Package Files

interfaces.go parallel.go qsort.go radixsort.go

Variables

var MaxProcs = 0

MaxProcs controls how many goroutines to start for large sorts. If 0, GOMAXPROCS will be used; if 1, all sorts will be serial.

func ByBytes Uses

func ByBytes(data BytesInterface)

ByBytes sorts data by a []byte key.

func ByInt64 Uses

func ByInt64(data Int64Interface)

ByInt64 sorts data by an int64 key.

func ByString Uses

func ByString(data StringInterface)

ByString sorts data by a string key.

func ByUint64 Uses

func ByUint64(data Uint64Interface)

ByUint64 sorts data by a uint64 key.

func Flip Uses

func Flip(data sort.Interface)

Flip reverses the order of items in a sort.Interface.

func Quicksort Uses

func Quicksort(data sort.Interface)

Quicksort performs a parallel quicksort on data.

type BytesInterface Uses

type BytesInterface interface {
    sort.Interface
    // Key provides the []byte key for element i.
    Key(i int) []byte
}

BytesInterface represents a collection that can be sorted by a []byte key.

type Int64Interface Uses

type Int64Interface interface {
    sort.Interface
    // Key provides an int64 key for element i.
    Key(i int) int64
}

Int64Interface represents a collection that can be sorted by an int64 key.

type StringInterface Uses

type StringInterface interface {
    sort.Interface
    // Key provides the string key for element i.
    Key(i int) string
}

StringInterface represents a collection that can be sorted by a string key.

type Uint64Interface Uses

type Uint64Interface interface {
    sort.Interface
    // Key provides a uint64 key for element i.
    Key(i int) uint64
}

Uint64Interface represents a collection that can be sorted by a uint64 key.

Directories

PathSynopsis
indexPackage index uses sorted arrays of integers to assist sorting and searching, particularly of collections of strings.
sortutilPackage sortutil sorts and searches common slice types (and helps sort floats).

Package sorts imports 4 packages (graph) and is imported by 2 packages. Updated 2016-08-15. Refresh now. Tools for package owners.