Skip to content

Commit d999452

Browse files
authored
weak ordering implementation in sort.slice (#1414)
Signed-off-by: rootp1 <arnav.iitr@gmail.com>
1 parent bf7e75c commit d999452

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

token-erc-1155/chaincode-go/chaincode/contract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,9 +1113,9 @@ func sortedKeysToID(m map[ToID]uint64) []ToID {
11131113
// Sort the slice first according to ID if equal then sort by recipient ("To" field)
11141114
sort.Slice(keys, func(i, j int) bool {
11151115
if keys[i].ID != keys[j].ID {
1116-
return keys[i].To < keys[j].To
1116+
return keys[i].ID < keys[j].ID
11171117
}
1118-
return keys[i].ID < keys[j].ID
1118+
return keys[i].To < keys[j].To
11191119
})
11201120
return keys
11211121
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package chaincode
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSortedKeysToID(t *testing.T) {
9+
testMap := map[ToID]uint64{
10+
{To: "Alice", ID: 2}: 100,
11+
{To: "Bob", ID: 1}: 200,
12+
{To: "Charlie", ID: 2}: 300,
13+
{To: "Alice", ID: 1}: 400,
14+
{To: "Bob", ID: 2}: 500,
15+
{To: "Charlie", ID: 1}: 600,
16+
}
17+
18+
expectedResult := []ToID{
19+
{To: "Alice", ID: 1},
20+
{To: "Bob", ID: 1},
21+
{To: "Charlie", ID: 1},
22+
{To: "Alice", ID: 2},
23+
{To: "Bob", ID: 2},
24+
{To: "Charlie", ID: 2},
25+
}
26+
27+
result := sortedKeysToID(testMap)
28+
29+
if !reflect.DeepEqual(result, expectedResult) {
30+
t.Fatalf("sortedKeysToID failed.\nExpected: %v\nGot: %v", expectedResult, result)
31+
}
32+
}

0 commit comments

Comments
 (0)