pkg/utils: Add function to sort sets of containers or images in JSON

https://github.com/containers/toolbox/pull/318
This commit is contained in:
Harry Míchal 2020-05-06 14:31:13 +02:00 committed by Debarshi Ray
parent 928bf53943
commit 74c2d7f02b

View file

@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/exec"
"sort"
"syscall"
"github.com/containers/toolbox/pkg/shell"
@ -208,3 +209,14 @@ func ShowManual(manual string) error {
return nil
}
func SortJSON(json []map[string]interface{}, key string, hasInterface bool) []map[string]interface{} {
sort.Slice(json, func(i, j int) bool {
if hasInterface {
return json[i][key].([]interface{})[0].(string) < json[j][key].([]interface{})[0].(string)
}
return json[i][key].(string) < json[j][key].(string)
})
return json
}