"Go 구조체 슬라이스 정렬"의 두 판 사이의 차이

 
36번째 줄: 36번째 줄:
==같이 보기==
==같이 보기==
* [[Go 구조체 슬라이스]]
* [[Go 구조체 슬라이스]]
* [[Go 슬라이스 정렬]]
* [[Go 맵 슬라이스 정렬]]
* [[Go 맵 슬라이스 정렬]]


[[분류: Go 구조체 슬라이스]]
[[분류: Go 구조체 슬라이스]]
[[분류: Go 정렬]]
[[분류: Go 정렬]]

2022년 5월 25일 (수) 12:24 기준 최신판

1 개요[ | ]

Go 구조체 슬라이스 정렬
package main

import (
	"fmt"
	"sort"
)

func main() {
	fruits := []struct {
		id    int
		name  string
		price int
	}{
		{102, "apple", 30},
		{202, "banana", 20},
		{104, "melon", 20},
	}
	sort.Slice(fruits, func(i, j int) bool {
		if fruits[i].price < fruits[j].price {
			return true
		}
		if fruits[i].price > fruits[j].price {
			return false
		}
		return fruits[i].id < fruits[j].id
	})
	fmt.Println(fruits)
}


2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}