Skip to content

Commit 37db288

Browse files
fix: user sorting
1 parent 3f00c82 commit 37db288

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

internal/asset/asset.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package asset
22

33
import (
4+
"strings"
5+
46
c "github.com/achannarasappa/ticker/v4/internal/common"
57
)
68

@@ -26,8 +28,21 @@ func GetAssets(ctx c.Context, assetGroupQuote c.AssetGroupQuote) ([]c.Asset, Hol
2628
var holdingSummary HoldingSummary
2729
assets := make([]c.Asset, 0)
2830
holdingsBySymbol := getLots(assetGroupQuote.AssetGroup.ConfigAssetGroup.Holdings)
31+
orderIndex := make(map[string]int)
32+
33+
for i, symbol := range assetGroupQuote.AssetGroup.ConfigAssetGroup.Watchlist {
34+
if _, exists := orderIndex[symbol]; !exists {
35+
orderIndex[strings.ToLower(symbol)] = i
36+
}
37+
}
38+
39+
for i, symbol := range assetGroupQuote.AssetGroup.ConfigAssetGroup.Holdings {
40+
if _, exists := orderIndex[symbol.Symbol]; !exists {
41+
orderIndex[strings.ToLower(symbol.Symbol)] = i + len(assetGroupQuote.AssetGroup.ConfigAssetGroup.Watchlist) - 1
42+
}
43+
}
2944

30-
for i, assetQuote := range assetGroupQuote.AssetQuotes {
45+
for _, assetQuote := range assetGroupQuote.AssetQuotes {
3146

3247
currencyRateByUse := getCurrencyRateByUse(ctx, assetQuote.Currency.FromCurrencyCode, assetQuote.Currency.ToCurrencyCode, assetQuote.Currency.Rate)
3348

@@ -50,7 +65,7 @@ func GetAssets(ctx c.Context, assetGroupQuote c.AssetGroupQuote) ([]c.Asset, Hol
5065
Exchange: assetQuote.Exchange,
5166
Meta: c.Meta{
5267
IsVariablePrecision: assetQuote.Meta.IsVariablePrecision,
53-
OrderIndex: i,
68+
OrderIndex: orderIndex[strings.ToLower(assetQuote.Symbol)],
5469
},
5570
})
5671

internal/sorter/sorter.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,16 @@ func NewSorter(sort string) Sorter {
2323
return sortByChange
2424
}
2525

26-
func sortByUser(assetsIn []*c.Asset) []*c.Asset {
26+
func sortByUser(assets []*c.Asset) []*c.Asset {
2727

28-
assetCount := len(assetsIn)
28+
assetCount := len(assets)
2929

3030
if assetCount <= 0 {
31-
return assetsIn
31+
return assets
3232
}
3333

34-
assets := make([]*c.Asset, assetCount)
35-
copy(assets, assetsIn)
36-
3734
sort.SliceStable(assets, func(i, j int) bool {
38-
39-
prevIndex := assetCount
40-
nextIndex := assetCount
41-
42-
if assets[i].Holding != (c.Holding{}) {
43-
prevIndex = assets[i].Meta.OrderIndex
44-
}
45-
46-
if assets[j].Holding != (c.Holding{}) {
47-
nextIndex = assets[j].Meta.OrderIndex
48-
}
49-
50-
return nextIndex > prevIndex
35+
return assets[j].Meta.OrderIndex > assets[i].Meta.OrderIndex
5136
})
5237

5338
return assets

internal/sorter/sorter_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ var _ = Describe("Sorter", func() {
4444
IsActive: true,
4545
IsRegularTradingSession: false,
4646
},
47+
Meta: c.Meta{
48+
OrderIndex: 2,
49+
},
4750
}
4851
googleQuote := c.Asset{
4952
Symbol: "GOOG",
@@ -76,6 +79,9 @@ var _ = Describe("Sorter", func() {
7679
IsActive: false,
7780
IsRegularTradingSession: false,
7881
},
82+
Meta: c.Meta{
83+
OrderIndex: 3,
84+
},
7985
}
8086
rblxQuote := c.Asset{
8187
Symbol: "RBLX",
@@ -89,6 +95,9 @@ var _ = Describe("Sorter", func() {
8995
IsActive: false,
9096
IsRegularTradingSession: false,
9197
},
98+
Meta: c.Meta{
99+
OrderIndex: 4,
100+
},
92101
}
93102
assets := []*c.Asset{
94103
&bitcoinQuote,

0 commit comments

Comments
 (0)