/
sort-sortFunc.go
139 lines (115 loc) · 2.91 KB
/
sort-sortFunc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// package main
// import (
// "fmt"
// "slices"
// )
// func main() {
// // 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"}
// slices.Sort(strs)
// fmt.Println("Strings:", strs)
// // An example of sorting `int`s.
// ints := []int{7, 2, 4}
// slices.Sort(ints)
// fmt.Println("Ints: ", 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)
// }
//----------------------------------------------------------------------------
// package main
// import (
// "fmt"
// "slices"
// "time"
// )
// func main() {
// datas := []time.Time{
// time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC),
// time.Date(2024, time.July, 20, 0, 0, 0, 0, time.UTC),
// time.Date(2024, time.January, 5, 0, 0, 0, 0, time.UTC),
// time.Date(2023, time.July, 20, 0, 0, 0, 0, time.UTC),
// }
// slices.SortFunc(datas, func(a, b time.Time) int {
// if a.Equal(b) {
// return 0
// }
// if a.Before(b) {
// return -1
// }
// return 1
// })
// fmt.Println("Datas ordenadas:", datas)
// }
//-----------------------------------------------------------------------
// package main
// import (
// "fmt"
// "slices"
// )
// type Pessoa struct {
// Nome string
// Idade int
// }
// func main() {
// pessoas := []Pessoa{
// {"Maria", 30},
// {"João", 25},
// {"Ana", 28},
// }
// // Ordenando por idade
// slices.SortFunc(pessoas, func(a, b Pessoa) int {
// return a.Idade - b.Idade // Retorna negativo se a < b, zero se a == b, positivo se a > b
// })
// fmt.Println("Pessoas ordenadas por idade:", pessoas)
// // Ordenando por nome
// slices.SortFunc(pessoas, func(a, b Pessoa) int {
// if a.Nome < b.Nome {
// return -1
// } else if a.Nome > b.Nome {
// return 1
// }
// return 0
// })
// fmt.Println("Pessoas ordenadas por nome:", pessoas)
// }
// ---------------------------------------------------------------------------------
package main
import (
"fmt"
"slices"
)
type Produto struct {
Nome string
Preço float64
}
func main() {
produtos := []Produto{
{"Caderno", 7.50},
{"Caneta", 3.75},
{"Mochila", 56.90},
}
// Ordenando por preço
slices.SortFunc(produtos, func(a, b Produto) int {
if a.Preço < b.Preço {
return -1
} else if a.Preço > b.Preço {
return 1
}
return 0
})
fmt.Println("Produtos ordenados por preço:", produtos)
// Ordenando por nome
slices.SortFunc(produtos, func(a, b Produto) int {
if a.Nome < b.Nome {
return -1
} else if a.Nome > b.Nome {
return 1
}
return 0
})
fmt.Println("Produtos ordenados por nome:", produtos)
}