aboutsummaryrefslogtreecommitdiff
path: root/src/sort
diff options
context:
space:
mode:
authorKarsten Köhler <karsten.koehler95@gmail.com>2017-08-21 21:13:53 +0200
committerJoe Tsai <thebrokentoaster@gmail.com>2017-08-28 17:29:29 +0000
commit9afec99dc2e354811ea4cf5ba2e2a7f9a5a8f1e4 (patch)
tree40c218f9076f95f826d6c69024f8839abb28693f /src/sort
parent627d3a0b4c153a4d5c8d53f18083e4c5c133d429 (diff)
downloadgo-9afec99dc2e354811ea4cf5ba2e2a7f9a5a8f1e4.tar.gz
go-9afec99dc2e354811ea4cf5ba2e2a7f9a5a8f1e4.zip
sort: add examples for IntsAreSorted, Float64s and Float64sAreSorted
Change-Id: Ib4883470bd2271e546daea3156d4a48dd873aaa3 Reviewed-on: https://go-review.googlesource.com/57670 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/sort')
-rw-r--r--src/sort/example_test.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/sort/example_test.go b/src/sort/example_test.go
index f8d8491bc4..1f85dbcbfb 100644
--- a/src/sort/example_test.go
+++ b/src/sort/example_test.go
@@ -6,6 +6,7 @@ package sort_test
import (
"fmt"
+ "math"
"sort"
)
@@ -16,6 +17,49 @@ func ExampleInts() {
// Output: [1 2 3 4 5 6]
}
+func ExampleIntsAreSorted() {
+ s := []int{1, 2, 3, 4, 5, 6} // sorted ascending
+ fmt.Println(sort.IntsAreSorted(s))
+
+ s = []int{6, 5, 4, 3, 2, 1} // sorted descending
+ fmt.Println(sort.IntsAreSorted(s))
+
+ s = []int{3, 2, 4, 1, 5} // unsorted
+ fmt.Println(sort.IntsAreSorted(s))
+
+ // Output: true
+ // false
+ // false
+}
+
+func ExampleFloat64s() {
+ s := []float64{5.2, -1.3, 0.7, -3.8, 2.6} // unsorted
+ sort.Float64s(s)
+ fmt.Println(s)
+
+ s = []float64{math.Inf(1), math.NaN(), math.Inf(-1), 0.0} // unsorted
+ sort.Float64s(s)
+ fmt.Println(s)
+
+ // Output: [-3.8 -1.3 0.7 2.6 5.2]
+ // [NaN -Inf 0 +Inf]
+}
+
+func ExampleFloat64sAreSorted() {
+ s := []float64{0.7, 1.3, 2.6, 3.8, 5.2} // sorted ascending
+ fmt.Println(sort.Float64sAreSorted(s))
+
+ s = []float64{5.2, 3.8, 2.6, 1.3, 0.7} // sorted descending
+ fmt.Println(sort.Float64sAreSorted(s))
+
+ s = []float64{5.2, 1.3, 0.7, 3.8, 2.6} // unsorted
+ fmt.Println(sort.Float64sAreSorted(s))
+
+ // Output: true
+ // false
+ // false
+}
+
func ExampleReverse() {
s := []int{5, 2, 6, 3, 1, 4} // unsorted
sort.Sort(sort.Reverse(sort.IntSlice(s)))