summaryrefslogtreecommitdiff
path: root/strings
diff options
context:
space:
mode:
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))
+}