github.com/jgbaldwinbrown/perf@v0.1.1/benchstat/sort.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package benchstat 6 7 import ( 8 "math" 9 "sort" 10 ) 11 12 // An Order defines a sort order for a table. 13 // It reports whether t.Rows[i] should appear before t.Rows[j]. 14 type Order func(t *Table, i, j int) bool 15 16 // ByName sorts tables by the Benchmark name column 17 func ByName(t *Table, i, j int) bool { 18 return t.Rows[i].Benchmark < t.Rows[j].Benchmark 19 } 20 21 // ByDelta sorts tables by the Delta column, 22 // reversing the order when larger is better (for "speed" results). 23 func ByDelta(t *Table, i, j int) bool { 24 return math.Abs(t.Rows[i].PctDelta)*float64(t.Rows[i].Change) < 25 math.Abs(t.Rows[j].PctDelta)*float64(t.Rows[j].Change) 26 } 27 28 // Reverse returns the reverse of the given order. 29 func Reverse(order Order) Order { 30 return func(t *Table, i, j int) bool { return order(t, j, i) } 31 } 32 33 // Sort sorts a Table t (in place) by the given order. 34 func Sort(t *Table, order Order) { 35 sort.SliceStable(t.Rows, func(i, j int) bool { return order(t, i, j) }) 36 }