Go 구조체 슬라이스 정렬

Jmnote (토론 | 기여)님의 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 }}