Basic Programming/Golang
Golang - 슬라이스 정렬
Stayner
2023. 7. 13. 00:51
728x90
int 슬라이스 정렬
import(
"fmt"
"sort"
)
func main()
{
s := []int{5,2,6,3,1,4}
sort.Ints(s) // 정렬 끝 [1,2,3,4,5,6]
}
구조체 슬라이스 정렬
import(
"fmt"
"sort"
)
type Student struct
{
Name string
Age int
}
type Students []Student
func (s Students) Len() int { return len(s) }
func (s Students) Less(i, j int) bool { return s[i].Age < s[j].Age }
func (s Students) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func main()
{
s := []Student
{
{"가", 31},
{"나", 52},
{"다", 42},
{"라", 38},
{"마", 20}
}
sort.Sort(Students(s))
}
728x90