Refactored sorter, reenabled grouping by advancing/declining stocks

master
Michael Dvorkin 12 years ago
parent ee2853cafc
commit 438f3f2f8a
  1. 64
      lib/layout.go
  2. 62
      lib/sorter.go

@ -8,7 +8,6 @@ import (
`regexp` `regexp`
`strings` `strings`
`text/template` `text/template`
`sort`
`time` `time`
) )
@ -141,10 +140,8 @@ func (self *Layout) Header(profile *Profile) string {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Layout) prettify(quotes *Quotes) []Stock { func (self *Layout) prettify(quotes *Quotes) []Stock {
var sorts []sort.Interface
pretty := make([]Stock, len(quotes.stocks)) pretty := make([]Stock, len(quotes.stocks))
//for i, q := range group(quotes) {
for i, q := range quotes.stocks { for i, q := range quotes.stocks {
pretty[i].Ticker = pad(q.Ticker, self.columns[0].width) pretty[i].Ticker = pad(q.Ticker, self.columns[0].width)
pretty[i].LastTrade = pad(currency(q.LastTrade), self.columns[1].width) pretty[i].LastTrade = pad(currency(q.LastTrade), self.columns[1].width)
@ -163,70 +160,39 @@ func (self *Layout) prettify(quotes *Quotes) []Stock {
pretty[i].MarketCap = pad(currency(q.MarketCapX), self.columns[14].width) pretty[i].MarketCap = pad(currency(q.MarketCapX), self.columns[14].width)
} }
if quotes.profile.Ascending { profile := quotes.profile
sorts = []sort.Interface{ new(Sorter).Initialize(profile).SortByCurrentColumn(pretty)
ByTickerAsc { pretty }, //
ByLastTradeAsc { pretty }, // Group stocks by advancing/declining unless sorted by Chanage or Change%
ByChangeAsc { pretty }, // in which case the grouping is done already.
ByChangePctAsc { pretty }, //
ByOpenAsc { pretty }, if profile.Grouped && (profile.SortColumn < 2 || profile.SortColumn > 3) {
ByLowAsc { pretty }, pretty = group(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 },
}
}
sort.Sort(sorts[quotes.profile.SortColumn])
return pretty return pretty
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func group(quotes *Quotes) []Stock { func group(stocks []Stock) []Stock {
if !quotes.profile.Grouped { grouped := make([]Stock, len(stocks))
return quotes.stocks
} else {
grouped := make([]Stock, len(quotes.stocks))
current := 0 current := 0
for _,stock := range quotes.stocks {
for _,stock := range stocks {
if strings.Index(stock.Change, "-") == -1 { if strings.Index(stock.Change, "-") == -1 {
grouped[current] = stock grouped[current] = stock
current++ current++
} }
} }
for _,stock := range quotes.stocks { for _,stock := range stocks {
if strings.Index(stock.Change, "-") != -1 { if strings.Index(stock.Change, "-") != -1 {
grouped[current] = stock grouped[current] = stock
current++ current++
} }
} }
return grouped return grouped
} }
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func arrow_for(column int, profile *Profile) string { func arrow_for(column int, profile *Profile) string {

@ -3,6 +3,7 @@
package mop package mop
import ( import (
`sort`
`strings` `strings`
`strconv` `strconv`
) )
@ -43,7 +44,6 @@ type ByDividendDesc struct { Sortable }
type ByYieldDesc struct { Sortable } type ByYieldDesc struct { Sortable }
type ByMarketCapDesc 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 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 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) } 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 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 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 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 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 } 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 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) } 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 { func c(str string) float32 {
trimmed := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1) trimmed := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1)
value, _ := strconv.ParseFloat(trimmed, 32) value, _ := strconv.ParseFloat(trimmed, 32)

Loading…
Cancel
Save