Help us shape the future of Go! Take the 2026 Go Developer Survey

msort

package module
v0.0.0-...-13997bc Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2019 License: MIT Imports: 2 Imported by: 0

README

Build Status Coverage Status godoc Go Report Card

go-msort

Utility library for golang sort package.

Documentation

Overview

Example (Struct)
package main

import (
	"fmt"
	"sort"
	"strings"

	"github.com/kamichidu/go-msort"
)

type User struct {
	Order int64

	Email string
}

func main() {
	users := []User{
		{4, "dev04@example.com"},
		{1, "dev01@example.com"},
		{3, "dev13@example.com"},
		{3, "dev03@example.com"},
		{0, "dev00@example.com"},
		{2, "dev02@example.com"},
		{0, "dev10@example.com"},
	}
	sorter := msort.NewBuilderFrom(users).
		ByFunc(func(i, j int) bool {
			return users[i].Order < users[j].Order
		}).
		ByFunc(func(i, j int) bool {
			return strings.Compare(users[i].Email, users[j].Email) < 0
		}).
		ToSorter()
	sort.Stable(sorter)
	fmt.Println("**ascending**")
	for i := range users {
		fmt.Printf("%d - %s\n", users[i].Order, users[i].Email)
	}
	sort.Stable(sort.Reverse(sorter))
	fmt.Println("**descending**")
	for i := range users {
		fmt.Printf("%d - %s\n", users[i].Order, users[i].Email)
	}
}
Output:
**ascending**
0 - dev00@example.com
0 - dev10@example.com
1 - dev01@example.com
2 - dev02@example.com
3 - dev03@example.com
3 - dev13@example.com
4 - dev04@example.com
**descending**
4 - dev04@example.com
3 - dev13@example.com
3 - dev03@example.com
2 - dev02@example.com
1 - dev01@example.com
0 - dev10@example.com
0 - dev00@example.com

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Less

func Less(i, j int, l ...LessFunc) bool

Types

type Builder

type Builder struct {
	SwapFn SwapFunc

	LenFn LenFunc
	// contains filtered or unexported fields
}

func NewBuilderFrom

func NewBuilderFrom(slice interface{}) Builder

func (Builder) ByFunc

func (b Builder) ByFunc(fn LessFunc) Builder

func (Builder) ToSorter

func (b Builder) ToSorter() sort.Interface

type LenFunc

type LenFunc func() int

type LessFunc

type LessFunc func(i, j int) bool

type SwapFunc

type SwapFunc func(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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