Skip to content

Commit 6353f3b

Browse files
committed
refactor: simplify sort functions
prefer Sort over SortFunc and use Cmp.or instead of Sprintf
1 parent fc8b37a commit 6353f3b

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

ledger/simulation/resources.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,17 +1066,13 @@ func (p *resourcePopulator) populateResources(groupResourceTracker ResourceTrack
10661066
for asset := range groupResourceTracker.Assets {
10671067
groupResources.Assets = append(groupResources.Assets, asset)
10681068
}
1069-
slices.SortFunc(groupResources.Assets, func(a, b basics.AssetIndex) int {
1070-
return cmp.Compare(a, b)
1071-
})
1069+
slices.Sort(groupResources.Assets)
10721070

10731071
// Sort apps
10741072
for app := range groupResourceTracker.Apps {
10751073
groupResources.Apps = append(groupResources.Apps, app)
10761074
}
1077-
slices.SortFunc(groupResources.Apps, func(a, b basics.AppIndex) int {
1078-
return cmp.Compare(a, b)
1079-
})
1075+
slices.Sort(groupResources.Apps)
10801076

10811077
// Sort accounts
10821078
for account := range groupResourceTracker.Accounts {
@@ -1086,31 +1082,25 @@ func (p *resourcePopulator) populateResources(groupResourceTracker ResourceTrack
10861082
return cmp.Compare(a.GetUserAddress(), b.GetUserAddress())
10871083
})
10881084

1089-
// Sort boxes
1090-
// To sort boxes, we turn the app into a string, concat with the name, and then sort
1091-
for box := range groupResourceTracker.Boxes {
1092-
groupResources.Boxes = append(groupResources.Boxes, box)
1093-
}
1085+
// Sort boxes by app first and then name
10941086
slices.SortFunc(groupResources.Boxes, func(a, b logic.BoxRef) int {
1095-
return cmp.Compare(fmt.Sprintf("%d:%s", a.App, a.Name), fmt.Sprintf("%d:%s", b.App, b.Name))
1087+
return cmp.Or(cmp.Compare(a.App, b.App), cmp.Compare(a.Name, b.Name))
10961088
})
10971089

1098-
// Sort assets holdings
1099-
// To sort asset holdings, we turn the asset into a string, concat with the address, and then sort
1090+
// Sort assets holdings by account first and then asset
11001091
for holding := range groupResourceTracker.AssetHoldings {
11011092
groupResources.AssetHoldings = append(groupResources.AssetHoldings, holding)
11021093
}
11031094
slices.SortFunc(groupResources.AssetHoldings, func(a, b ledgercore.AccountAsset) int {
1104-
return cmp.Compare(fmt.Sprintf("%d:%s", a.Asset, a.Address), fmt.Sprintf("%d:%s", b.Asset, b.Address.GetUserAddress()))
1095+
return cmp.Or(cmp.Compare(a.Address.GetUserAddress(), b.Address.GetUserAddress()), cmp.Compare(a.Asset, b.Asset))
11051096
})
11061097

1107-
// Sort app locals
1108-
// To sort app locals, we turn the app into a string, concat with the address, and then sort
1098+
// Sort app locals by account first and then app
11091099
for local := range groupResourceTracker.AppLocals {
11101100
groupResources.AppLocals = append(groupResources.AppLocals, local)
11111101
}
11121102
slices.SortFunc(groupResources.AppLocals, func(a, b ledgercore.AccountApp) int {
1113-
return cmp.Compare(fmt.Sprintf("%d:%s", a.App, a.Address), fmt.Sprintf("%d:%s", b.App, b.Address.GetUserAddress()))
1103+
return cmp.Or(cmp.Compare(a.Address.GetUserAddress(), b.Address.GetUserAddress()), cmp.Compare(a.App, b.App))
11141104
})
11151105

11161106
// First populate resources that HAVE to be assigned to a specific transaction
@@ -1120,18 +1110,14 @@ func (p *resourcePopulator) populateResources(groupResourceTracker ResourceTrack
11201110
for asset := range tracker.Assets {
11211111
sortedAssets = append(sortedAssets, asset)
11221112
}
1123-
slices.SortFunc(sortedAssets, func(a, b basics.AssetIndex) int {
1124-
return cmp.Compare(a, b)
1125-
})
1113+
slices.Sort(sortedAssets)
11261114

11271115
// Sort apps
11281116
sortedApps := make([]basics.AppIndex, 0, len(tracker.Apps))
11291117
for app := range tracker.Apps {
11301118
sortedApps = append(sortedApps, app)
11311119
}
1132-
slices.SortFunc(sortedApps, func(a, b basics.AppIndex) int {
1133-
return cmp.Compare(a, b)
1134-
})
1120+
slices.Sort(sortedApps)
11351121

11361122
// Sort accounts
11371123
sortedAccounts := make([]basics.Address, 0, len(tracker.Accounts))

0 commit comments

Comments
 (0)