1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-25 23:02:22 +02:00

сделал MassFrom_Map_DESC()

This commit is contained in:
Nikitin Aleksandr
2024-11-29 09:35:31 +03:00
parent 1a02361c36
commit 8ba0b6188b
2 changed files with 49 additions and 0 deletions

View File

@@ -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)

View File

@@ -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"