Skip to content

Instantly share code, notes, and snippets.

@vil1
Created August 31, 2011 08:25
Show Gist options
  • Save vil1/1183070 to your computer and use it in GitHub Desktop.
Save vil1/1183070 to your computer and use it in GitHub Desktop.
package main
import (
"sort"
"fmt"
)
type Element struct {
a int
b string
}
type SortableByAAsc []Element
func (s SortableByAAsc) Len() int { return len(s)}
func (s SortableByAAsc) Swap(i, j int) { s[i], s[j] = s[j], s[i]}
func (s SortableByAAsc) Less(i, j int) bool { return s[i].a <= s[j].a }
type SortableByADesc []Element
func (s SortableByADesc) Len() int { return len(s)}
func (s SortableByADesc) Swap(i, j int) { s[i], s[j] = s[j], s[i]}
func (s SortableByADesc) Less(i, j int) bool { return s[i].a >= s[j].a }
type SortableByBLenAsc []Element
func (s SortableByBLenAsc) Len() int { return len(s)}
func (s SortableByBLenAsc) Swap(i, j int) { s[i], s[j] = s[j], s[i]}
func (s SortableByBLenAsc) Less(i, j int) bool { return len(s[i].b) <= len(s[j].b) }
var slice []Element = []Element{
Element { 1 , "foo" },
Element { 42, "blah" },
Element { 0, "a" },
}
func main() {
sort.Sort(SortableByAAsc(slice))
fmt.Println(slice)
sort.Sort(SortableByADesc(slice))
fmt.Println(slice)
sort.Sort(SortableByBLenAsc(slice))
fmt.Println(slice)
}
package main
import (
"sort"
"fmt"
)
type Element struct {
a int
b string
}
type ElementSlice []Element
func (coll ElementSlice) Len() int { return len(coll)}
func (coll ElementSlice) Swap(i, j int) { coll[i], coll[j] = coll[j], coll[i]}
type SortableByAAsc struct { ElementSlice }
func (s SortableByAAsc) Less(i, j int) bool { return s.ElementSlice[i].a <= s.ElementSlice[j].a }
type SortableByADesc struct { ElementSlice }
func (s SortableByADesc) Less(i, j int) bool { return s.ElementSlice[i].a >= s.ElementSlice[j].a }
type SortableByBLenAsc struct { ElementSlice }
func (s SortableByBLenAsc) Less(i, j int) bool { return len(s.ElementSlice[i].b) <= len(s.ElementSlice[j].b) }
var slice ElementSlice = ElementSlice{
Element { 1 , "foo" },
Element { 42, "blah" },
Element { 0, "a" },
}
func main() {
sort.Sort(SortableByAAsc{slice})
fmt.Println(slice)
sort.Sort(SortableByADesc{slice})
fmt.Println(slice)
sort.Sort(SortableByBLenAsc{slice})
fmt.Println(slice)
}
package main
import (
"sort"
"fmt"
)
type Element struct {
a int
b string
}
type SortableElementSlice struct {
e []Element
f func(int, int) bool
}
func (s SortableElementSlice) Len() int { return len(s.e) }
func (s SortableElementSlice) Swap(i, j int) { s.e[i], s.e[j] = s.e[j], s.e[i] }
func (s SortableElementSlice) Less(i, j int) bool {
if s.f != nil {
return s.f(i, j)
}
// provide a useful default
return s.e[i].a <= s.e[j].a
}
func (s SortableElementSlice) SortBy(f func(int, int) bool) {
s.f = f
sort.Sort(s)
}
var slice SortableElementSlice = SortableElementSlice{
e: []Element{
Element { 1 , "foo" },
Element { 42, "blah" },
Element { 0, "a" },
},
f: nil,
}
func main() {
sort.Sort(slice)
fmt.Println(slice.e)
slice.SortBy(func(i, j int) bool { return slice.e[i].a >= slice.e[j].a })
fmt.Println(slice.e)
slice.SortBy(func(i, j int) bool { return len(slice.e[i].b) <= len(slice.e[j].b)})
fmt.Println(slice.e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment