github.com/moleculer-go/moleculer@v0.3.3/test/snapshot.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  )
     7  
     8  // OrderMapArray given an array of map[string]... this func will order based on the field name.
     9  func OrderMapArray(list []map[string]interface{}, field string) []map[string]interface{} {
    10  	result := make([]map[string]interface{}, len(list))
    11  	keys := make([]string, len(list))
    12  	positions := make(map[string]int)
    13  	for index, item := range list {
    14  		keys[index] = fmt.Sprint(item[field])
    15  		positions[keys[index]] = index
    16  	}
    17  	sort.Strings(keys)
    18  	for index, key := range keys {
    19  		position := positions[key]
    20  		result[index] = list[position]
    21  	}
    22  	return result
    23  }