From 0b0114ad800fc3c01994015b1a2c8137579a4def Mon Sep 17 00:00:00 2001 From: Dustin Long Date: Mon, 20 May 2024 10:10:39 -0400 Subject: [PATCH] Sort keys returned by AllKeys in reverse order --- viper.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 4f1580c..0287b98 100644 --- a/viper.go +++ b/viper.go @@ -29,6 +29,7 @@ import ( "os" "path/filepath" "reflect" + "sort" "strings" "time" @@ -1801,9 +1802,20 @@ func (v *Viper) AllKeys() []string { for x := range m { a = append(a, x) } - return a + sort.Strings(a) + return ReverseStringSlice(a) } +func ReverseStringSlice(array []string) []string { + length := len(array) + result := make([]string, length) + for i, elem := range array { + result[length-1-i] = elem + } + return result +} + + // flattenAndMergeMap recursively flattens the given map into a map[string]bool // of key paths (used as a set, easier to manipulate than a []string): // - each path is merged into a single key string, delimited with v.keyDelim (= ".")