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

(새 문서: ==개요== ;Go 구조체 슬라이스 정렬 <syntaxhighlight lang='go' run> package main import ( "fmt" "sort" ) func main() { fruits := []struct { id int name string...)
 
39번째 줄: 39번째 줄:


[[분류: 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 }}