diff --git a/examples/sorting/sorting.go b/examples/sorting/sorting.go index acc67d8d7..f022982a6 100644 --- a/examples/sorting/sorting.go +++ b/examples/sorting/sorting.go @@ -1,4 +1,4 @@ -// Go's `sort` package implements sorting for builtins +// Go's `slices` package implements sorting for builtins // and user-defined types. We'll look at sorting for // builtins first. @@ -6,26 +6,25 @@ package main import ( "fmt" - "sort" + "slices" ) func main() { - // Sort methods are specific to the builtin type; - // here's an example for strings. Note that sorting is - // in-place, so it changes the given slice and doesn't - // return a new one. + // Sorting functions are generic, and work for any + // _ordered_ built-in type. For a list of ordered + // types, see [cmp.Ordered](https://pkg.go.dev/cmp#Ordered). strs := []string{"c", "a", "b"} - sort.Strings(strs) + slices.Sort(strs) fmt.Println("Strings:", strs) // An example of sorting `int`s. ints := []int{7, 2, 4} - sort.Ints(ints) + slices.Sort(ints) fmt.Println("Ints: ", ints) - // We can also use `sort` to check if a slice is - // already in sorted order. - s := sort.IntsAreSorted(ints) + // We can also use the `slices` package to check if + // a slice is already in sorted order. + s := slices.IsSorted(ints) fmt.Println("Sorted: ", s) } diff --git a/examples/sorting/sorting.hash b/examples/sorting/sorting.hash index e845bdf24..083264320 100644 --- a/examples/sorting/sorting.hash +++ b/examples/sorting/sorting.hash @@ -1,2 +1,2 @@ -c39a7498686fe1d74f729fd6b21a70bf063abf14 -_gY0tANzJ4l +2091224c8d8ac748883215c4dbe9611fb8afacc3 +X7iJcIua02T diff --git a/examples/sorting/sorting.sh b/examples/sorting/sorting.sh index c0e74620f..838724656 100644 --- a/examples/sorting/sorting.sh +++ b/examples/sorting/sorting.sh @@ -1,5 +1,3 @@ -# Running our program prints the sorted string and int -# slices and `true` as the result of our `AreSorted` test. $ go run sorting.go Strings: [a b c] Ints: [2 4 7] diff --git a/public/sorting b/public/sorting index 866ca5dec..cd946ab27 100644 --- a/public/sorting +++ b/public/sorting @@ -27,7 +27,7 @@ -

Go’s sort package implements sorting for builtins +

Go’s slices package implements sorting for builtins and user-defined types. We’ll look at sorting for builtins first.

@@ -43,7 +43,7 @@ builtins first.

- +
package main
@@ -56,7 +56,7 @@ builtins first.

import (
     "fmt"
-    "sort"
+    "slices"
 )
@@ -73,16 +73,15 @@ builtins first.

-

Sort methods are specific to the builtin type; -here’s an example for strings. Note that sorting is -in-place, so it changes the given slice and doesn’t -return a new one.

+

Sorting functions are generic, and work for any +ordered built-in type. For a list of ordered +types, see cmp.Ordered.

    strs := []string{"c", "a", "b"}
-    sort.Strings(strs)
+    slices.Sort(strs)
     fmt.Println("Strings:", strs)
@@ -95,20 +94,20 @@ return a new one.

    ints := []int{7, 2, 4}
-    sort.Ints(ints)
+    slices.Sort(ints)
     fmt.Println("Ints:   ", ints)
-

We can also use sort to check if a slice is -already in sorted order.

+

We can also use the slices package to check if +a slice is already in sorted order.

-
    s := sort.IntsAreSorted(ints)
+          
    s := slices.IsSorted(ints)
     fmt.Println("Sorted: ", s)
 }
@@ -120,9 +119,7 @@ already in sorted order.

-

Running our program prints the sorted string and int -slices and true as the result of our AreSorted test.

- + @@ -148,7 +145,7 @@ slices and true as the result of our AreSorted test.