idxsort

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

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

Go to latest
Published: Mar 28, 2018 License: MIT Imports: 1 Imported by: 0

README

go-indexed-sort

A Go package for using sequences compatible with sort.Interface to sort other sequences.

Status

Current master branch should be considered a "release candidate" with interfaces and type signatures subject to change.

I will tag a 1.0 release when I am satisfied with usability and test coverage.

Usage

Use a slice of ints to sort a slice of structs:

var people []Person
// ... populate with 4 people ...
swapPeople := func(i, j int) {
	people[i], people[j] = people[j], people[i]
}
ageIndex := []int{32,56,19,24}
idxsort.SortAsc(sort.IntSlice(ageIndex), swapPeople)

sort.IntSlice attaches the methods of sort.Interface to 'ageIndex'. Slice 'ageIndex' is then sorted by ascending value, with index swaps duplicated in slice 'people' by the 'swapPeople' closure.

If your linked sequence implements sort.Interface, you can use its Swap function:

names := []string{"Amy","Bob","Carla","Dan"}
ageIndex := []int{32,56,19,24}
idxsort.SortAsc(sort.IntSlice(ageIndex), sort.StringSlice(names).Swap)

Online GoDoc

https://godoc.org/github.com/momokatte/go-indexed-sort

Documentation

Overview

Package idxsort provides types and functions for sorting sequences via separate indexes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sort

func Sort(index sort.Interface, ascending bool, swapFunc func(i, j int))

Sorts an index in the provided order, and duplicates index swaps to swapFunc.

func SortAsc

func SortAsc(index sort.Interface, swapFunc func(i, j int))

Sorts an index in ascending order, and duplicates index swaps to swapFunc.

func SortDesc

func SortDesc(index sort.Interface, swapFunc func(i, j int))

Sorts an index in descending order, and duplicates index swaps to swapFunc.

func SortIntStringMap

func SortIntStringMap(m map[int]string, ascending bool) (is []int, ss []string)

func SortStringFloat64Map

func SortStringFloat64Map(m map[string]float64, ascending bool) (ss []string, fs []float64)

func SortStringInt64Map

func SortStringInt64Map(m map[string]int64, ascending bool) (ss []string, is []int64)

func SortStringIntMap

func SortStringIntMap(m map[string]int, ascending bool) (ss []string, is []int)

func SortStringUint64Map

func SortStringUint64Map(m map[string]uint64, ascending bool) (ss []string, is []uint64)

Types

type Int64Slice

type Int64Slice []int64

Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.

func (Int64Slice) Len

func (s Int64Slice) Len() int

func (Int64Slice) Less

func (s Int64Slice) Less(i, j int) bool

func (Int64Slice) Sort

func (s Int64Slice) Sort()

Sort is a convenience method.

func (Int64Slice) Swap

func (s Int64Slice) Swap(i, j int)

type Sorter

type Sorter struct {
	// contains filtered or unexported fields
}

Sorter wraps a sequence implementing the sort.Interface interface, supports ascending and descending sort, and duplicates index swaps to other sequences via a swap function.

Methods of Sorter are not safe for concurrent use. The provided index and/or swap functions may also require explicit locking to prevent concurrent access.

Sorter implements the sort.Interface interface.

func NewSorter

func NewSorter(index sort.Interface, swapFunc func(i, j int)) (s *Sorter)

NewSorter instantiates a new Sorter with the provided index and swap function.

func (*Sorter) Len

func (s *Sorter) Len() int

func (*Sorter) Less

func (s *Sorter) Less(i, j int) bool

func (*Sorter) Sort

func (s *Sorter) Sort(ascending bool)

Sorts the index in ascending or descending order.

func (*Sorter) SortAsc

func (s *Sorter) SortAsc()

Sorts the index in ascending order.

func (*Sorter) SortDesc

func (s *Sorter) SortDesc()

Sorts the index in descending order.

func (*Sorter) Swap

func (s *Sorter) Swap(i, j int)

type Uint64Slice

type Uint64Slice []uint64

Uint64Slice attaches the methods of Interface to []uint64, sorting in increasing order.

func (Uint64Slice) Len

func (s Uint64Slice) Len() int

func (Uint64Slice) Less

func (s Uint64Slice) Less(i, j int) bool

func (Uint64Slice) Sort

func (s Uint64Slice) Sort()

Sort is a convenience method.

func (Uint64Slice) Swap

func (s Uint64Slice) Swap(i, j int)

Jump to

Keyboard shortcuts

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