Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Nov 5, 2023
1 parent 7cdf0fc commit 006126c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions slice_go1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com

import "sort"

type Number interface {
~uint8 | ~int8 | ~uint16 | ~int16 | ~uint32 | ~int32 | ~uint | ~int | ~uint64 | ~int64 | ~float32 | ~float64
}
Expand All @@ -22,3 +24,16 @@ func SliceExtractCallback[T Scalar](parts []string, cb func(string) T, recv ...*
*recv[index] = cb(value)
}
}

type reverseSortIndex[T any] []T

func (s reverseSortIndex[T]) Len() int { return len(s) }
func (s reverseSortIndex[T]) Less(i, j int) bool {
return j < i
}
func (s reverseSortIndex[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func ReverseSortIndex[T any](values []T) []T {
sort.Sort(reverseSortIndex[T](values))
return values
}
8 changes: 8 additions & 0 deletions slice_go1.18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func TestSliceExtractCallback(t *testing.T) {
assert.Equal(t, 2, n2N)
assert.Equal(t, 3, n3N)
}

func TestReverseSortIndex(t *testing.T) {
parts := []string{`1`, `2`, `3`}
ReverseSortIndex(parts)
assert.Equal(t, `3`, parts[0])
assert.Equal(t, `2`, parts[1])
assert.Equal(t, `1`, parts[2])
}

0 comments on commit 006126c

Please sign in to comment.