반응형
sort.Ints() 함수를 사용하여 슬라이스를 오름차순으로 정렬할 수 있다.
예제)
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{7, 2, 9, 1, 6, 3, 8, 5, 4}
sort.Ints(nums)
fmt.Println(nums)
}
// [1 2 3 4 5 6 7 8 9]
sort.Reverse() 함수를 사용하여 내림차순으로 정렬할 수 있다.
예제)
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{5, 2, 8, 1, 3, 9, 4, 6, 7}
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
fmt.Println(nums)
}
// [9 8 7 6 5 4 3 2 1]
반응형
'Golang' 카테고리의 다른 글
디스코드가 Go 대신 Rust로 전환하는 이유 (0) | 2023.06.14 |
---|---|
Golang 웹 크롤링/크롤러 (0) | 2023.06.01 |
Golang UUID 생성 (0) | 2023.03.27 |
Golang 임시 파일 생성(os.CreateTemp) (0) | 2023.03.27 |
Golang CSV를 JSON 변환 (0) | 2023.03.15 |
댓글