sort.Sort 메서드는 sort.Interface 정렬 인터페이스를 인자로 받는다.
정렬 인터페이스는 아래 3가지 Len(), Less(), Swap() 메서드를 구현하면 해당 구조체를 정렬할 수 있다.
func Sort(data Interface) {
n := data.Len()
quickSort(data, 0, n, maxDepth(n))
}
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
점수 순으로 정렬하기 위해서 Score type을 정의한다. 정의한 type에 대해서 Len(), Less(), Swap() 세가지 메서드를 구현한다.
type Score struct {
Cluster string
Score float32
}
type ScoreTable []Score
func (s ScoreTable) Len() int {
return len(s)
}
func (s ScoreTable) Less(i, j int) bool {
return s[i].Score < s[j].Score
}
func (s ScoreTable) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
✅ value값으로 오름차순 정렬하기
정렬하고 싶은 map의 length에 맞는 struct 배열을 만든다. (예제 : sorted)
map에 있는 값을 구조체 배열에 입력한 후, sort 패키지의 Sort 함수를 이용해 정렬한다.
var score_table = make(map[string]float32) // 정렬 대상 map
func SortScore() ScoreTable {
// 1. 구조체 배열 만들기
sorted := make(ScoreTable, len(score_table))
// 2. map에 있는 값을 구조채 배열에 입력하기
for cluster, score := range score_table {
sorted = append(sorted, Score{cluster, score})
}
// 3. sort 패키지의 Sort함수를 이용해 정렬하기
sort.Sort(sorted)
return sorted
}
test 화면
func main(){
score_table["a-cluster"] = 30.0
score_table["b-cluster"] = 40.0
score_table["c-cluster"] = 20.0
score_table["d-cluster"] = 50.0
score_sort := SortScore()
for _, score := range score_sort {
fmt.Printf("%s %2.2f\n", score.Cluster, score.Score)
}
}
--------------------------------------
c-cluster 20.00
a-cluster 30.00
b-cluster 40.00
d-cluster 50.00
✅ value값으로 내림차순 정렬하기
sort.Sort 함수 인자로 Sort.Reverse()를 넣어주면 된다.
func SortScore() ScoreTable {
sorted := make(ScoreTable, len(score_table))
for cluster, score := range score_table {
sorted = append(sorted, Score{cluster, score})
}
sort.Sort(sort.Reverse(sorted))
return sorted
}
test 화면
func main(){
score_table["a-cluster"] = 30.0
score_table["b-cluster"] = 40.0
score_table["c-cluster"] = 20.0
score_table["d-cluster"] = 50.0
score_sort := SortScore()
for _, score := range score_sort {
fmt.Printf("%s %2.2f\n", score.Cluster, score.Score)
}
}
--------------------------------------
d-cluster 50.00
b-cluster 40.00
a-cluster 30.00
c-cluster 20.00
✅ key 값으로 정렬하기
map의 key값으로 정렬하고 싶은 경우, sort의 Sort함수 대신 Strings 함수를 이용하면된다.
func SortCluster() []string {
sorted := make([]string, 0, len(score_table))
for cluster := range score_table {
sorted = append(sorted, cluster)
}
sort.Strings(sorted)
return sorted
}
test화면
func main(){
score_table["a-cluster"] = 30.0
score_table["b-cluster"] = 40.0
score_table["c-cluster"] = 20.0
score_table["d-cluster"] = 50.0
cluster_sort := SortCluster()
for _, cluster := range cluster_sort {
fmt.Printf("%s\n", cluster)
}
}
-------------------------------------------
a-cluster
b-cluster
c-cluster
d-cluster
반응형
'Programming language > Golang' 카테고리의 다른 글
[Go] Method (0) | 2022.09.08 |
---|---|
[Go] ch16 패키지 (0) | 2022.01.18 |
[Go] ch15 문자열 (0) | 2022.01.18 |
[Go] ch14 포인터 (0) | 2022.01.15 |
[Go] ch13 구조체 (0) | 2022.01.15 |