Skip to content

joyt/sortx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

sortx

Sortx is a simple golang sort extension that provides a method for sorting an array of structs by a field.

Installation:

go get github.com/joyt/sortx

Example:

import (
	"time"
	"github.com/joyt/sortx"
)
type Person struct {
	Name string
	Age int
	BirthDate time.Time
	Married bool
	Popularity float64
}

func main() {

	people := []Person{
		{Name: "Santa Claus", Age: 424, BirthDate: time.Date(1590, 12, 25, 0,0,0,0, time.Local()), Married: true, Popularity: 95.7},
		{Name: "Easter Bunny", Age: 231, BirthDate:  time.Date(1783, 4, 5, 0,0,0,0, time.Local()), Married: false, Popularity: 20.4},
		{Name: "Cupid", Age: 1250, time.Date(764, 2, 14, 0,0,0,0, time.Local()), Married: false, Popularity: 55.5},
		{Name: "Tooth Fairy", Age: 104, BirthDate: time.Date(1910, 2, 14, 0,0,0,0, time.Local()), Married: true, Popularity: 32.1},
	}

	// Sort by name alphabectially
	sortx.SortByField(people, "Name", sortx.Ascending)

	// Sort by birth date, oldest first.
	sortx.SortByField(people, "BirthDate", sortx.Ascending)

	// Sort by age, oldest first
	sortx.SortByField(people, "Age", sortx.Descending)

	// Sort by popularity
	sortx.SortByField(people, "Popularity", sortx.Descending)
}

About

Sort by a field in a struct

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages