diff --git a/micro/microfunctions.go b/micro/microfunctions.go index e2e820f9..1866b4e7 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -1194,6 +1194,13 @@ func SortMass[T constraints.Ordered](s []T) { }) } +// SortMass_DESC - сортирует слайс, в обратном порядке +func SortMass_DESC[T constraints.Ordered](s []T) { + sort.Slice(s, func(i, j int) bool { + return s[i] > s[j] + }) +} + // MassFrom_Map - сортирует map по названию колонок и возвращает слайс func MassFrom_Map[C constraints.Ordered, V any](Map map[C]V) []V { Otvet := make([]V, 0) @@ -1217,6 +1224,29 @@ func MassFrom_Map[C constraints.Ordered, V any](Map map[C]V) []V { return Otvet } +// MassFrom_Map_DESC - сортирует map по названию колонок и возвращает слайс, с обратной сортировкой +func MassFrom_Map_DESC[C constraints.Ordered, V any](Map map[C]V) []V { + Otvet := make([]V, 0) + + //сортировка по названию колонок + keys := make([]C, 0, len(Map)) + for k := range Map { + keys = append(keys, k) + } + SortMass_DESC(keys) + + // + for _, key1 := range keys { + Value, ok := Map[key1] + if ok == false { + fmt.Printf("Map[%v] not found\n", key1) + } + Otvet = append(Otvet, Value) + } + + return Otvet +} + // Substring - take at most last n characters, from start index func Substring(input string, StartIndex int, length int) string { //asRunes := []rune(input) diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index d91a8bd2..ebcd3ae5 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -1190,6 +1190,25 @@ func TestMassFrom_Map(t *testing.T) { }) } +func TestMassFrom_Map_DESC(t *testing.T) { + // Test case for sorting a map by column names + t.Run("Sort map by keys", func(t *testing.T) { + input := map[string]int{"c": 3, "a": 1, "b": 2} + expected := []int{3, 2, 1} + result := MassFrom_Map_DESC(input) + if !reflect.DeepEqual(result, expected) { + t.Errorf("Expected %v, but got %v", expected, result) + } + }) + + // Test case for handling a key not found in the map + t.Run("Key not found", func(t *testing.T) { + input := map[string]string{"a": "apple", "b": "banana"} + _ = MassFrom_Map(input) // This call should print an error message for the missing key + // You can capture the output of fmt.Printf using https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string + }) +} + func TestStringDateTime(t *testing.T) { // Test case 1: Testing the output format for a specific date and time expectedResult1 := "31.12.2022 23:59:59"