Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(713)

Side by Side Diff: common/data/sortby/sortby.go

Issue 2657733002: Add sort function helper library. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | common/data/sortby/sortby_test.go » ('j') | common/data/sortby/sortby_test.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « no previous file | common/data/sortby/sortby_test.go » ('j') | common/data/sortby/sortby_test.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698