Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2020 Jebbs. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
 
package merge
 
import "sort"
 
func getPriority(v interface{}) float64 {
    var m map[string]interface{}
    var ok bool
    if m, ok = v.(map[string]interface{}); !ok {
        return 0
    }
    if i, ok := m[priorityKey]; ok {
        if p, ok := i.(float64); ok {
            return p
        }
    }
    return 0
}
 
// sortByPriority sort slice by priority fields of their elements
func sortByPriority(slice []interface{}) {
    sort.Slice(
        slice,
        func(i, j int) bool {
            return getPriority(slice[i]) < getPriority(slice[j])
        },
    )
}