summaryrefslogtreecommitdiff
path: root/strings
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-07-18 21:35:33 -0400
committerDave Henderson <dhenderson@gmail.com>2018-07-18 21:35:33 -0400
commitc56dc624778fc3f9695a712c02c8b306da0c88b2 (patch)
treead4c9ac281511680014a465654ace05f3759ba78 /strings
parent458c19fba9b7183bf60504af6005c7fc82fc3815 (diff)
Adding new strings.Sort function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'strings')
-rw-r--r--strings/strings.go12
-rw-r--r--strings/strings_test.go14
2 files changed, 25 insertions, 1 deletions
diff --git a/strings/strings.go b/strings/strings.go
index 3b90c83e..6d89def7 100644
--- a/strings/strings.go
+++ b/strings/strings.go
@@ -1,6 +1,9 @@
package strings
-import "strings"
+import (
+ "sort"
+ "strings"
+)
// Indent - indent each line of the string with the given indent string
func Indent(width int, indent, s string) string {
@@ -33,3 +36,10 @@ func Trunc(length int, s string) string {
}
return s[0:length]
}
+
+// Sort - return an alphanumerically-sorted list of strings
+func Sort(list []string) []string {
+ sorted := sort.StringSlice(list)
+ sorted.Sort()
+ return sorted
+}
diff --git a/strings/strings_test.go b/strings/strings_test.go
index ad32e3f6..f2afb2f5 100644
--- a/strings/strings_test.go
+++ b/strings/strings_test.go
@@ -25,3 +25,17 @@ func TestTrunc(t *testing.T) {
assert.Equal(t, "hello, world", Trunc(42, "hello, world"))
assert.Equal(t, "hello, world", Trunc(-1, "hello, world"))
}
+
+func TestSort(t *testing.T) {
+ in := []string{}
+ expected := []string{}
+ assert.EqualValues(t, expected, Sort(in))
+
+ in = []string{"c", "a", "b"}
+ expected = []string{"a", "b", "c"}
+ assert.EqualValues(t, expected, Sort(in))
+
+ in = []string{"42", "45", "18"}
+ expected = []string{"18", "42", "45"}
+ assert.EqualValues(t, expected, Sort(in))
+}