Files
obitools4/pkg/obistats/sort.go
Eric Coissac c7ed47e110 first version of obidemerge, obijoin and a new filter for obicleandb but to be finnished
Former-commit-id: 8a1ed26e5548c30db75644c294d478ec4d753f19
2024-07-10 15:21:42 +02:00

38 lines
1.0 KiB
Go

package obistats
//
// Dupplicated code from internal module available at :
// https://github.com/golang-design/bench.git
//
import (
"math"
"sort"
)
// An Order defines a sort order for a table.
// It reports whether t.Rows[i] should appear before t.Rows[j].
type Order func(t *Table, i, j int) bool
// ByName sorts tables by the Benchmark name column
func ByName(t *Table, i, j int) bool {
return t.Rows[i].Benchmark < t.Rows[j].Benchmark
}
// ByDelta sorts tables by the Delta column,
// reversing the order when larger is better (for "speed" results).
func ByDelta(t *Table, i, j int) bool {
return math.Abs(t.Rows[i].PctDelta)*float64(t.Rows[i].Change) <
math.Abs(t.Rows[j].PctDelta)*float64(t.Rows[j].Change)
}
// Reverse returns the reverse of the given order.
func Reverse(order Order) Order {
return func(t *Table, i, j int) bool { return order(t, j, i) }
}
// Sort sorts a Table t (in place) by the given order.
func Sort(t *Table, order Order) {
sort.SliceStable(t.Rows, func(i, j int) bool { return order(t, i, j) })
}