Go 맵 슬라이스 정렬

Jmnote (토론 | 기여)님의 2022년 5월 19일 (목) 14:52 판 (→‎개요)

1 개요

Go 맵 슬라이스 정렬
package main

import (
	"fmt"
	"sort"

	"github.com/spf13/cast"
)

func main() {
	fruits := []map[string]interface{}{
		{"id": 102, "name": "apple", "price": 30},
		{"id": 202, "name": "banana", "price": 10},
		{"id": 104, "name": "melon", "price": 20},
	}
	sort.Slice(fruits, func(i, j int) bool {
		return cast.ToInt(fruits[i]["price"]) < cast.ToInt(fruits[j]["price"])
	})
	fmt.Println(fruits)

}

2 같이 보기

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