Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 // Package sortby provides a succinct way to generate correctly-behaved Less | |
| 6 // funcions for use with the stdlib 'sort' package. | |
| 7 package sortby | |
| 8 | |
| 9 // LessFn is the type of the function which compares element i with element j of | |
| 10 // a given slice. Unlike the stdlib sort interpretation of this function, | |
| 11 // a LessFn in sortby should only compare a single field in your datastructure's | |
| 12 // elements. Multiple LessFns can be composed with This and ThenBy to create | |
|
Vadim Sh.
2017/01/25 03:43:34
what This and ThenBy?
iannucci
2017/01/25 03:47:24
Removed them (generated extra unnecessary indirect
| |
| 13 // a composite Less implementation to pass to sort. | |
| 14 type LessFn func(i, j int) bool | |
| 15 | |
| 16 // Chain is a list of LessFns, each of which sorts a single aspect of your | |
| 17 // object. Nil LessFns will be ignored. | |
| 18 type Chain []LessFn | |
| 19 | |
| 20 // Use is a sort-compatible LessFn that actually executes the full chain of | |
| 21 // comparisons. | |
| 22 func (c Chain) Use(i, j int) bool { | |
| 23 for _, less := range c { | |
| 24 if less == nil { | |
| 25 continue | |
| 26 } | |
| 27 if less(i, j) { | |
| 28 return true | |
| 29 } else if less(j, i) { | |
| 30 return false | |
| 31 } | |
| 32 } | |
| 33 return false | |
| 34 } | |
| OLD | NEW |