|
@@ -9,7 +9,7 @@ Use one of the functions
|
|
|
* [`sort.Float64s`](https://golang.org/pkg/sort/#Float64s)
|
|
* [`sort.Float64s`](https://golang.org/pkg/sort/#Float64s)
|
|
|
* [`sort.Strings`](https://golang.org/pkg/sort/#Strings)
|
|
* [`sort.Strings`](https://golang.org/pkg/sort/#Strings)
|
|
|
|
|
|
|
|
-```
|
|
|
|
|
|
|
+```go
|
|
|
s := []int{4, 2, 3, 1}
|
|
s := []int{4, 2, 3, 1}
|
|
|
sort.Ints(s)
|
|
sort.Ints(s)
|
|
|
fmt.Println(s) // [1 2 3 4]
|
|
fmt.Println(s) // [1 2 3 4]
|
|
@@ -26,7 +26,7 @@ fmt.Println(s) // [1 2 3 4]
|
|
|
* To sort the slice while keeping the original order of equal elements,
|
|
* To sort the slice while keeping the original order of equal elements,
|
|
|
use [`sort.SliceStable`](https://golang.org/pkg/sort/#SliceStable) instead.
|
|
use [`sort.SliceStable`](https://golang.org/pkg/sort/#SliceStable) instead.
|
|
|
|
|
|
|
|
-```
|
|
|
|
|
|
|
+```go
|
|
|
family := []struct {
|
|
family := []struct {
|
|
|
Name string
|
|
Name string
|
|
|
Age int
|
|
Age int
|
|
@@ -52,7 +52,7 @@ fmt.Println(family) // [{David 2} {Eve 2} {Alice 23} {Bob 25}]
|
|
|
[`sort.Interface`](https://golang.org/pkg/sort/#Interface)
|
|
[`sort.Interface`](https://golang.org/pkg/sort/#Interface)
|
|
|
[interface](https://yourbasic.org/golang/interfaces-explained/).
|
|
[interface](https://yourbasic.org/golang/interfaces-explained/).
|
|
|
|
|
|
|
|
-```
|
|
|
|
|
|
|
+```go
|
|
|
type Interface interface {
|
|
type Interface interface {
|
|
|
// Len is the number of elements in the collection.
|
|
// Len is the number of elements in the collection.
|
|
|
Len() int
|
|
Len() int
|
|
@@ -66,7 +66,7 @@ type Interface interface {
|
|
|
|
|
|
|
|
Here’s an example.
|
|
Here’s an example.
|
|
|
|
|
|
|
|
-```
|
|
|
|
|
|
|
+```go
|
|
|
type Person struct {
|
|
type Person struct {
|
|
|
Name string
|
|
Name string
|
|
|
Age int
|
|
Age int
|
|
@@ -98,7 +98,7 @@ you must maintain a separate data structure.
|
|
|
|
|
|
|
|
This code example uses a slice of keys to sort a map in key order.
|
|
This code example uses a slice of keys to sort a map in key order.
|
|
|
|
|
|
|
|
-```
|
|
|
|
|
|
|
+```go
|
|
|
m := map[string]int{"Alice": 2, "Cecil": 1, "Bob": 3}
|
|
m := map[string]int{"Alice": 2, "Cecil": 1, "Bob": 3}
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(m))
|
|
keys := make([]string, 0, len(m))
|