go sort연습

chacha·2020년 7월 24일
0

go알고리즘

목록 보기
5/8

source

package main
import (
	"fmt"
	"sort"
)
type myDataType struct {
	name string
	age  int
}
func main() {
	mySlice := make([]myDataType, 0)
	mySlice = append(mySlice, myDataType{"김형준", 42})
	mySlice = append(mySlice, myDataType{"홍길동", 28})
	mySlice = append(mySlice, myDataType{"임꺽정", 38})
	fmt.Println(mySlice)
	sort.Slice(mySlice, func(i, j int) bool {
		return mySlice[i].age < mySlice[j].age
	})
	fmt.Println(mySlice)
}

output

[{김형준 42} {홍길동 28} {임꺽정 38}]
[{홍길동 28} {임꺽정 38} {김형준 42}]
profile
안녕하세요~ :)

0개의 댓글