mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
weak ordering implementation in sort.slice (#1414)
Signed-off-by: rootp1 <arnav.iitr@gmail.com>
This commit is contained in:
parent
bf7e75c6c1
commit
d999452f88
2 changed files with 34 additions and 2 deletions
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
32
token-erc-1155/chaincode-go/chaincode/contract_test.go
Normal file
32
token-erc-1155/chaincode-go/chaincode/contract_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue