priority.go 637 B

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