tros
provides methods to sort slices of structs using reflection
type Thing struct {
Name string
Weight, Height int
Awesome bool
}
func sortThingsByName(things []Thing) {
sort.Sort(byName(things))
}
type byName []Thing
func (s byName) Len() int { return len(s) }
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byName) Less(i, j int) bool { return s[i].Name < s[j].Name } // Sort by name
// TODO: Support sorting by weight, height, awesomeness, etc.
Sorting slices of Go structs idiomatically is easy, if you only intend to do it for a few different fields, and you know what they are ahead of time. Sorting by arbitrary fields means copypasting lots of boilerplate with small nuanced changes.
type Thing struct {
Name string
Weight, Height int
Awesome bool
}
func sortThings(things []Thing) {
fmt.Println("Sorted by name:")
tros.Sort(things, "Name")
fmt.Println(things)
fmt.Println("Sorted by weight:")
tros.Sort(things, "Weight")
fmt.Println(things)
// Sort in reverse order too!
fmt.Println("Sorted by name (reversed):")
tros.Sort(things, "-Name")
fmt.Println(things)
// ...and so on
}
With tros
, you can easily sort slices of structs by the values of arbitrary fields described at runtime, leaving out lots of boring boilerplate.
If you want to define your own custom sorting logic, you can have your struct fields implement the Lesser
interface, which tros
will use to determine ordering.
tros
uses reflection, and as such is noticeably (~2.5x) slower for sorting than the idiomatic Go way. But for small-to-medium slices, the difference should be negligible. If performance is more important than convenience, use the standardsort
package
Copyright 2014 Jason Hall
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.