Sorting by market cap

master
Michael Dvorkin 11 years ago
parent 5dfff606c1
commit ee2853cafc
  1. 6
      lib/layout.go
  2. 41
      lib/sorter.go

@ -29,7 +29,7 @@ func (self *Layout) Initialize() *Layout {
{ -7, `Ticker` }, { -7, `Ticker` },
{ 10, `Last` }, { 10, `Last` },
{ 10, `Change` }, { 10, `Change` },
{ 10, `%Change`}, { 10, `Change%` },
{ 10, `Open` }, { 10, `Open` },
{ 10, `Low` }, { 10, `Low` },
{ 10, `High` }, { 10, `High` },
@ -177,11 +177,9 @@ func (self *Layout) prettify(quotes *Quotes) []Stock {
ByVolumeAsc { pretty }, ByVolumeAsc { pretty },
ByAvgVolumeAsc { pretty }, ByAvgVolumeAsc { pretty },
ByPeRatioAsc { pretty }, ByPeRatioAsc { pretty },
//ByPeRatioXAsc { pretty },
ByDividendAsc { pretty }, ByDividendAsc { pretty },
ByYieldAsc { pretty }, ByYieldAsc { pretty },
ByMarketCapAsc { pretty }, ByMarketCapAsc { pretty },
//ByMarketCapXAsc { pretty },
} }
} else { } else {
sorts = []sort.Interface{ sorts = []sort.Interface{
@ -197,11 +195,9 @@ func (self *Layout) prettify(quotes *Quotes) []Stock {
ByVolumeDesc { pretty }, ByVolumeDesc { pretty },
ByAvgVolumeDesc { pretty }, ByAvgVolumeDesc { pretty },
ByPeRatioDesc { pretty }, ByPeRatioDesc { pretty },
//ByPeRatioXDesc { pretty },
ByDividendDesc { pretty }, ByDividendDesc { pretty },
ByYieldDesc { pretty }, ByYieldDesc { pretty },
ByMarketCapDesc { pretty }, ByMarketCapDesc { pretty },
//ByMarketCapXDesc { pretty },
} }
} }

@ -23,11 +23,9 @@ type ByHigh52Asc struct { Sortable }
type ByVolumeAsc struct { Sortable } type ByVolumeAsc struct { Sortable }
type ByAvgVolumeAsc struct { Sortable } type ByAvgVolumeAsc struct { Sortable }
type ByPeRatioAsc struct { Sortable } type ByPeRatioAsc struct { Sortable }
type ByPeRatioXAsc struct { Sortable }
type ByDividendAsc struct { Sortable } type ByDividendAsc struct { Sortable }
type ByYieldAsc struct { Sortable } type ByYieldAsc struct { Sortable }
type ByMarketCapAsc struct { Sortable } type ByMarketCapAsc struct { Sortable }
type ByMarketCapXAsc struct { Sortable }
type ByTickerDesc struct { Sortable } type ByTickerDesc struct { Sortable }
type ByLastTradeDesc struct { Sortable } type ByLastTradeDesc struct { Sortable }
@ -41,17 +39,15 @@ type ByHigh52Desc struct { Sortable }
type ByVolumeDesc struct { Sortable } type ByVolumeDesc struct { Sortable }
type ByAvgVolumeDesc struct { Sortable } type ByAvgVolumeDesc struct { Sortable }
type ByPeRatioDesc struct { Sortable } type ByPeRatioDesc struct { Sortable }
type ByPeRatioXDesc struct { Sortable }
type ByDividendDesc struct { Sortable } type ByDividendDesc struct { Sortable }
type ByYieldDesc struct { Sortable } type ByYieldDesc struct { Sortable }
type ByMarketCapDesc struct { Sortable } type ByMarketCapDesc struct { Sortable }
type ByMarketCapXDesc 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 z(list.Sortable[i].Change) < z(list.Sortable[j].Change) } func (list ByChangeAsc) Less(i, j int) bool { return c(list.Sortable[i].Change) < c(list.Sortable[j].Change) }
func (list ByChangePctAsc) Less(i, j int) bool { return z(list.Sortable[i].ChangePct) < z(list.Sortable[j].ChangePct) } func (list ByChangePctAsc) Less(i, j int) bool { return c(list.Sortable[i].ChangePct) < c(list.Sortable[j].ChangePct) }
func (list ByOpenAsc) Less(i, j int) bool { return list.Sortable[i].Open < list.Sortable[j].Open } func (list ByOpenAsc) Less(i, j int) bool { return list.Sortable[i].Open < list.Sortable[j].Open }
func (list ByLowAsc) Less(i, j int) bool { return list.Sortable[i].Low < list.Sortable[j].Low } func (list ByLowAsc) Less(i, j int) bool { return list.Sortable[i].Low < list.Sortable[j].Low }
func (list ByHighAsc) Less(i, j int) bool { return list.Sortable[i].High < list.Sortable[j].High } func (list ByHighAsc) Less(i, j int) bool { return list.Sortable[i].High < list.Sortable[j].High }
@ -60,16 +56,14 @@ func (list ByHigh52Asc) Less(i, j int) bool { return list.Sortable[i].High
func (list ByVolumeAsc) Less(i, j int) bool { return list.Sortable[i].Volume < list.Sortable[j].Volume } func (list ByVolumeAsc) Less(i, j int) bool { return list.Sortable[i].Volume < list.Sortable[j].Volume }
func (list ByAvgVolumeAsc) Less(i, j int) bool { return list.Sortable[i].AvgVolume < list.Sortable[j].AvgVolume } func (list ByAvgVolumeAsc) Less(i, j int) bool { return list.Sortable[i].AvgVolume < list.Sortable[j].AvgVolume }
func (list ByPeRatioAsc) Less(i, j int) bool { return list.Sortable[i].PeRatio < list.Sortable[j].PeRatio } func (list ByPeRatioAsc) Less(i, j int) bool { return list.Sortable[i].PeRatio < list.Sortable[j].PeRatio }
func (list ByPeRatioXAsc) Less(i, j int) bool { return list.Sortable[i].PeRatioX < list.Sortable[j].PeRatioX }
func (list ByDividendAsc) Less(i, j int) bool { return list.Sortable[i].Dividend < list.Sortable[j].Dividend } func (list ByDividendAsc) Less(i, j int) bool { return list.Sortable[i].Dividend < list.Sortable[j].Dividend }
func (list ByYieldAsc) Less(i, j int) bool { return list.Sortable[i].Yield < list.Sortable[j].Yield } func (list ByYieldAsc) Less(i, j int) bool { return list.Sortable[i].Yield < list.Sortable[j].Yield }
func (list ByMarketCapAsc) Less(i, j int) bool { return list.Sortable[i].MarketCap < list.Sortable[j].MarketCap } func (list ByMarketCapAsc) Less(i, j int) bool { return m(list.Sortable[i].MarketCap) < m(list.Sortable[j].MarketCap) }
func (list ByMarketCapXAsc) Less(i, j int) bool { return list.Sortable[i].MarketCapX < list.Sortable[j].MarketCapX }
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 z(list.Sortable[j].Change) < z(list.Sortable[i].Change) } func (list ByChangeDesc) Less(i, j int) bool { return c(list.Sortable[j].Change) < c(list.Sortable[i].Change) }
func (list ByChangePctDesc) Less(i, j int) bool { return z(list.Sortable[j].ChangePct) < z(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 }
func (list ByHighDesc) Less(i, j int) bool { return list.Sortable[j].High < list.Sortable[i].High } func (list ByHighDesc) Less(i, j int) bool { return list.Sortable[j].High < list.Sortable[i].High }
@ -78,14 +72,27 @@ func (list ByHigh52Desc) Less(i, j int) bool { return list.Sortable[j].High
func (list ByVolumeDesc) Less(i, j int) bool { return list.Sortable[j].Volume < list.Sortable[i].Volume } func (list ByVolumeDesc) Less(i, j int) bool { return list.Sortable[j].Volume < list.Sortable[i].Volume }
func (list ByAvgVolumeDesc) Less(i, j int) bool { return list.Sortable[j].AvgVolume < list.Sortable[i].AvgVolume } func (list ByAvgVolumeDesc) Less(i, j int) bool { return list.Sortable[j].AvgVolume < list.Sortable[i].AvgVolume }
func (list ByPeRatioDesc) Less(i, j int) bool { return list.Sortable[j].PeRatio < list.Sortable[i].PeRatio } func (list ByPeRatioDesc) Less(i, j int) bool { return list.Sortable[j].PeRatio < list.Sortable[i].PeRatio }
func (list ByPeRatioXDesc) Less(i, j int) bool { return list.Sortable[j].PeRatioX < list.Sortable[i].PeRatioX }
func (list ByDividendDesc) Less(i, j int) bool { return list.Sortable[j].Dividend < list.Sortable[i].Dividend } func (list ByDividendDesc) Less(i, j int) bool { return list.Sortable[j].Dividend < list.Sortable[i].Dividend }
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 list.Sortable[j].MarketCap < list.Sortable[i].MarketCap } func (list ByMarketCapDesc) Less(i, j int) bool { return m(list.Sortable[j].MarketCap) < m(list.Sortable[i].MarketCap) }
func (list ByMarketCapXDesc) Less(i, j int) bool { return list.Sortable[j].MarketCapX < list.Sortable[i].MarketCapX }
func z(str string) float32 { func c(str string) float32 {
float := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1) trimmed := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1)
value,_ := strconv.ParseFloat(float, 32) value, _ := strconv.ParseFloat(trimmed, 32)
return float32(value) return float32(value)
} }
func m(str string) float32 {
multiplier := 1.0
switch str[len(str)-1:len(str)] {
case `B`:
multiplier = 1000000000.0
case `M`:
multiplier = 1000000.0
case `K`:
multiplier = 1000.0
}
trimmed := strings.Trim(str, ` $BMK`)
value, _ := strconv.ParseFloat(trimmed, 32)
return float32(value * multiplier)
}

Loading…
Cancel
Save