diff --git a/lib/layout.go b/lib/layout.go index 21848de..0bc4c19 100644 --- a/lib/layout.go +++ b/lib/layout.go @@ -8,7 +8,6 @@ import ( `regexp` `strings` `text/template` - `sort` `time` ) @@ -141,10 +140,8 @@ func (self *Layout) Header(profile *Profile) string { //----------------------------------------------------------------------------- func (self *Layout) prettify(quotes *Quotes) []Stock { - var sorts []sort.Interface pretty := make([]Stock, len(quotes.stocks)) - //for i, q := range group(quotes) { for i, q := range quotes.stocks { pretty[i].Ticker = pad(q.Ticker, self.columns[0].width) pretty[i].LastTrade = pad(currency(q.LastTrade), self.columns[1].width) @@ -163,69 +160,38 @@ func (self *Layout) prettify(quotes *Quotes) []Stock { pretty[i].MarketCap = pad(currency(q.MarketCapX), self.columns[14].width) } - if quotes.profile.Ascending { - sorts = []sort.Interface{ - ByTickerAsc { pretty }, - ByLastTradeAsc { pretty }, - ByChangeAsc { pretty }, - ByChangePctAsc { pretty }, - ByOpenAsc { pretty }, - ByLowAsc { pretty }, - ByHighAsc { pretty }, - ByLow52Asc { pretty }, - ByHigh52Asc { pretty }, - ByVolumeAsc { pretty }, - ByAvgVolumeAsc { pretty }, - ByPeRatioAsc { pretty }, - ByDividendAsc { pretty }, - ByYieldAsc { pretty }, - ByMarketCapAsc { pretty }, - } - } else { - sorts = []sort.Interface{ - ByTickerDesc { pretty }, - ByLastTradeDesc { pretty }, - ByChangeDesc { pretty }, - ByChangePctDesc { pretty }, - ByOpenDesc { pretty }, - ByLowDesc { pretty }, - ByHighDesc { pretty }, - ByLow52Desc { pretty }, - ByHigh52Desc { pretty }, - ByVolumeDesc { pretty }, - ByAvgVolumeDesc { pretty }, - ByPeRatioDesc { pretty }, - ByDividendDesc { pretty }, - ByYieldDesc { pretty }, - ByMarketCapDesc { pretty }, - } + profile := quotes.profile + new(Sorter).Initialize(profile).SortByCurrentColumn(pretty) + // + // Group stocks by advancing/declining unless sorted by Chanage or Change% + // in which case the grouping is done already. + // + if profile.Grouped && (profile.SortColumn < 2 || profile.SortColumn > 3) { + pretty = group(pretty) } - sort.Sort(sorts[quotes.profile.SortColumn]) return pretty } //----------------------------------------------------------------------------- -func group(quotes *Quotes) []Stock { - if !quotes.profile.Grouped { - return quotes.stocks - } else { - grouped := make([]Stock, len(quotes.stocks)) - current := 0 - for _,stock := range quotes.stocks { - if strings.Index(stock.Change, "-") == -1 { - grouped[current] = stock - current++ - } +func group(stocks []Stock) []Stock { + grouped := make([]Stock, len(stocks)) + current := 0 + + for _,stock := range stocks { + if strings.Index(stock.Change, "-") == -1 { + grouped[current] = stock + current++ } - for _,stock := range quotes.stocks { - if strings.Index(stock.Change, "-") != -1 { - grouped[current] = stock - current++ - } + } + for _,stock := range stocks { + if strings.Index(stock.Change, "-") != -1 { + grouped[current] = stock + current++ } - return grouped } + + return grouped } //----------------------------------------------------------------------------- diff --git a/lib/sorter.go b/lib/sorter.go index 884be89..c240cb3 100644 --- a/lib/sorter.go +++ b/lib/sorter.go @@ -3,6 +3,7 @@ package mop import ( + `sort` `strings` `strconv` ) @@ -43,7 +44,6 @@ type ByDividendDesc struct { Sortable } type ByYieldDesc struct { Sortable } type ByMarketCapDesc struct { Sortable } - func (list ByTickerAsc) Less(i, j int) bool { return list.Sortable[i].Ticker < list.Sortable[j].Ticker } func (list ByLastTradeAsc) Less(i, j int) bool { return list.Sortable[i].LastTrade < list.Sortable[j].LastTrade } func (list ByChangeAsc) Less(i, j int) bool { return c(list.Sortable[i].Change) < c(list.Sortable[j].Change) } @@ -62,7 +62,7 @@ func (list ByMarketCapAsc) Less(i, j int) bool { return m(list.Sortable[i].Ma func (list ByTickerDesc) Less(i, j int) bool { return list.Sortable[j].Ticker < list.Sortable[i].Ticker } func (list ByLastTradeDesc) Less(i, j int) bool { return list.Sortable[j].LastTrade < list.Sortable[i].LastTrade } -func (list ByChangeDesc) Less(i, j int) bool { return c(list.Sortable[j].Change) < c(list.Sortable[i].Change) } +func (list ByChangeDesc) Less(i, j int) bool { return c(list.Sortable[j].ChangePct) < c(list.Sortable[i].ChangePct) } func (list ByChangePctDesc) Less(i, j int) bool { return c(list.Sortable[j].ChangePct) < c(list.Sortable[i].ChangePct) } func (list ByOpenDesc) Less(i, j int) bool { return list.Sortable[j].Open < list.Sortable[i].Open } func (list ByLowDesc) Less(i, j int) bool { return list.Sortable[j].Low < list.Sortable[i].Low } @@ -76,6 +76,64 @@ func (list ByDividendDesc) Less(i, j int) bool { return list.Sortable[j].Divi func (list ByYieldDesc) Less(i, j int) bool { return list.Sortable[j].Yield < list.Sortable[i].Yield } func (list ByMarketCapDesc) Less(i, j int) bool { return m(list.Sortable[j].MarketCap) < m(list.Sortable[i].MarketCap) } +type Sorter struct { + profile *Profile +} + +func (self *Sorter) Initialize(profile *Profile) *Sorter { + self.profile = profile + + return self +} + +func (self *Sorter) SortByCurrentColumn(stocks []Stock) *Sorter { + var interfaces []sort.Interface + + if self.profile.Ascending { + interfaces = []sort.Interface{ + ByTickerAsc { stocks }, + ByLastTradeAsc { stocks }, + ByChangeAsc { stocks }, + ByChangePctAsc { stocks }, + ByOpenAsc { stocks }, + ByLowAsc { stocks }, + ByHighAsc { stocks }, + ByLow52Asc { stocks }, + ByHigh52Asc { stocks }, + ByVolumeAsc { stocks }, + ByAvgVolumeAsc { stocks }, + ByPeRatioAsc { stocks }, + ByDividendAsc { stocks }, + ByYieldAsc { stocks }, + ByMarketCapAsc { stocks }, + } + } else { + interfaces = []sort.Interface{ + ByTickerDesc { stocks }, + ByLastTradeDesc { stocks }, + ByChangeDesc { stocks }, + ByChangePctDesc { stocks }, + ByOpenDesc { stocks }, + ByLowDesc { stocks }, + ByHighDesc { stocks }, + ByLow52Desc { stocks }, + ByHigh52Desc { stocks }, + ByVolumeDesc { stocks }, + ByAvgVolumeDesc { stocks }, + ByPeRatioDesc { stocks }, + ByDividendDesc { stocks }, + ByYieldDesc { stocks }, + ByMarketCapDesc { stocks }, + } + } + + sort.Sort(interfaces[self.profile.SortColumn]) + + return self +} + +// The same exact method is used to sort by Change and Change%. In both cases +// we sort by the value of Change% so that $0.00 change gets sorted proferly. func c(str string) float32 { trimmed := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1) value, _ := strconv.ParseFloat(trimmed, 32)