최신판 |
당신의 편집 |
22번째 줄: |
22번째 줄: |
| }) | | }) |
| fmt.Println(fruits) | | fmt.Println(fruits) |
| }
| |
| </syntaxhighlight>
| |
| <syntaxhighlight lang='go' run>
| |
| 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": 20},
| |
| {"id": 104, "name": "melon", "price": 20},
| |
| }
| |
| sort.Slice(fruits, func(i, j int) bool {
| |
| if cast.ToInt(fruits[i]["price"]) < cast.ToInt(fruits[j]["price"]) {
| |
| return true
| |
| }
| |
| if cast.ToInt(fruits[i]["price"]) > cast.ToInt(fruits[j]["price"]) {
| |
| return false
| |
| }
| |
| return cast.ToInt(fruits[i]["id"]) < cast.ToInt(fruits[j]["id"])
| |
| })
| |
| fmt.Println(fruits)
| |
| } | | } |
| </syntaxhighlight> | | </syntaxhighlight> |