-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update sorting sample to use slices.Sort instead of sort package
- Loading branch information
Showing
4 changed files
with
25 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
// 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. | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
c39a7498686fe1d74f729fd6b21a70bf063abf14 | ||
_gY0tANzJ4l | ||
2091224c8d8ac748883215c4dbe9611fb8afacc3 | ||
X7iJcIua02T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.