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

Unified Diff: common/data/sortby/sortby.go

Issue 2657733002: Add sort function helper library. (Closed)
Patch Set: copyright 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | common/data/sortby/sortby_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/data/sortby/sortby.go
diff --git a/common/data/sortby/sortby.go b/common/data/sortby/sortby.go
new file mode 100644
index 0000000000000000000000000000000000000000..0786f3c582d0b7175e63660eb96dddc71bc67fef
--- /dev/null
+++ b/common/data/sortby/sortby.go
@@ -0,0 +1,34 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+// Package sortby provides a succinct way to generate correctly-behaved Less
+// funcions for use with the stdlib 'sort' package.
+package sortby
+
+// LessFn is the type of the function which compares element i with element j of
+// a given slice. Unlike the stdlib sort interpretation of this function,
+// a LessFn in sortby should only compare a single field in your datastructure's
+// elements. Multiple LessFns can be composed with Chain to create a composite
+// Less implementation to pass to sort.
+type LessFn func(i, j int) bool
+
+// Chain is a list of LessFns, each of which sorts a single aspect of your
+// object. Nil LessFns will be ignored.
+type Chain []LessFn
+
+// Use is a sort-compatible LessFn that actually executes the full chain of
+// comparisons.
+func (c Chain) Use(i, j int) bool {
+ for _, less := range c {
+ if less == nil {
+ continue
+ }
+ if less(i, j) {
+ return true
+ } else if less(j, i) {
+ return false
+ }
+ }
+ return false
+}
« no previous file with comments | « no previous file | common/data/sortby/sortby_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698