weak ordering implementation in sort.slice (#1414)

Signed-off-by: rootp1 <arnav.iitr@gmail.com>
This commit is contained in:
Arnav Vinod Deshpande 2026-05-05 16:17:26 +05:30 committed by GitHub
parent bf7e75c6c1
commit d999452f88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 2 deletions

View file

@ -1113,9 +1113,9 @@ func sortedKeysToID(m map[ToID]uint64) []ToID {
// Sort the slice first according to ID if equal then sort by recipient ("To" field) // Sort the slice first according to ID if equal then sort by recipient ("To" field)
sort.Slice(keys, func(i, j int) bool { sort.Slice(keys, func(i, j int) bool {
if keys[i].ID != keys[j].ID { if keys[i].ID != keys[j].ID {
return keys[i].To < keys[j].To
}
return keys[i].ID < keys[j].ID return keys[i].ID < keys[j].ID
}
return keys[i].To < keys[j].To
}) })
return keys return keys
} }

View file

@ -0,0 +1,32 @@
package chaincode
import (
"reflect"
"testing"
)
func TestSortedKeysToID(t *testing.T) {
testMap := map[ToID]uint64{
{To: "Alice", ID: 2}: 100,
{To: "Bob", ID: 1}: 200,
{To: "Charlie", ID: 2}: 300,
{To: "Alice", ID: 1}: 400,
{To: "Bob", ID: 2}: 500,
{To: "Charlie", ID: 1}: 600,
}
expectedResult := []ToID{
{To: "Alice", ID: 1},
{To: "Bob", ID: 1},
{To: "Charlie", ID: 1},
{To: "Alice", ID: 2},
{To: "Bob", ID: 2},
{To: "Charlie", ID: 2},
}
result := sortedKeysToID(testMap)
if !reflect.DeepEqual(result, expectedResult) {
t.Fatalf("sortedKeysToID failed.\nExpected: %v\nGot: %v", expectedResult, result)
}
}