From ee2853cafc9b3251b43a15e521322d46c6034da6 Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Sun, 28 Jul 2013 14:34:25 -0700 Subject: [PATCH] Sorting by market cap --- lib/layout.go | 34 +++++++++++++++------------------- lib/sorter.go | 43 +++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/lib/layout.go b/lib/layout.go index b6b086a..21848de 100644 --- a/lib/layout.go +++ b/lib/layout.go @@ -26,21 +26,21 @@ type Layout struct { //----------------------------------------------------------------------------- func (self *Layout) Initialize() *Layout { self.columns = []Column{ - { -7, `Ticker`}, - { 10, `Last`}, - { 10, `Change`}, - { 10, `%Change`}, - { 10, `Open`}, - { 10, `Low`}, - { 10, `High`}, - { 10, `52w Low`}, - { 10, `52w High`}, - { 11, `Volume`}, - { 11, `AvgVolume`}, - { 10, `P/E`}, - { 10, `Dividend`}, - { 10, `Yield`}, - { 11, `MktCap`}, + { -7, `Ticker` }, + { 10, `Last` }, + { 10, `Change` }, + { 10, `Change%` }, + { 10, `Open` }, + { 10, `Low` }, + { 10, `High` }, + { 10, `52w Low` }, + { 10, `52w High` }, + { 11, `Volume` }, + { 11, `AvgVolume` }, + { 10, `P/E` }, + { 10, `Dividend` }, + { 10, `Yield` }, + { 11, `MktCap` }, } return self @@ -177,11 +177,9 @@ func (self *Layout) prettify(quotes *Quotes) []Stock { ByVolumeAsc { pretty }, ByAvgVolumeAsc { pretty }, ByPeRatioAsc { pretty }, - //ByPeRatioXAsc { pretty }, ByDividendAsc { pretty }, ByYieldAsc { pretty }, ByMarketCapAsc { pretty }, - //ByMarketCapXAsc { pretty }, } } else { sorts = []sort.Interface{ @@ -197,11 +195,9 @@ func (self *Layout) prettify(quotes *Quotes) []Stock { ByVolumeDesc { pretty }, ByAvgVolumeDesc { pretty }, ByPeRatioDesc { pretty }, - //ByPeRatioXDesc { pretty }, ByDividendDesc { pretty }, ByYieldDesc { pretty }, ByMarketCapDesc { pretty }, - //ByMarketCapXDesc { pretty }, } } diff --git a/lib/sorter.go b/lib/sorter.go index 27d15ae..884be89 100644 --- a/lib/sorter.go +++ b/lib/sorter.go @@ -23,11 +23,9 @@ type ByHigh52Asc struct { Sortable } type ByVolumeAsc struct { Sortable } type ByAvgVolumeAsc struct { Sortable } type ByPeRatioAsc struct { Sortable } -type ByPeRatioXAsc struct { Sortable } type ByDividendAsc struct { Sortable } type ByYieldAsc struct { Sortable } type ByMarketCapAsc struct { Sortable } -type ByMarketCapXAsc struct { Sortable } type ByTickerDesc struct { Sortable } type ByLastTradeDesc struct { Sortable } @@ -41,17 +39,15 @@ type ByHigh52Desc struct { Sortable } type ByVolumeDesc struct { Sortable } type ByAvgVolumeDesc struct { Sortable } type ByPeRatioDesc struct { Sortable } -type ByPeRatioXDesc struct { Sortable } type ByDividendDesc struct { Sortable } type ByYieldDesc 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 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 ByChangePctAsc) Less(i, j int) bool { return z(list.Sortable[i].ChangePct) < z(list.Sortable[j].ChangePct) } +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 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 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 } @@ -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 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 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 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 ByMarketCapXAsc) Less(i, j int) bool { return list.Sortable[i].MarketCapX < list.Sortable[j].MarketCapX } +func (list ByMarketCapAsc) Less(i, j int) bool { return m(list.Sortable[i].MarketCap) < m(list.Sortable[j].MarketCap) } 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 z(list.Sortable[j].Change) < z(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 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 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 } 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 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 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 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 ByMarketCapXDesc) Less(i, j int) bool { return list.Sortable[j].MarketCapX < list.Sortable[i].MarketCapX } +func (list ByMarketCapDesc) Less(i, j int) bool { return m(list.Sortable[j].MarketCap) < m(list.Sortable[i].MarketCap) } -func z(str string) float32 { - float := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1) - value,_ := strconv.ParseFloat(float, 32) +func c(str string) float32 { + trimmed := strings.Replace(strings.Trim(str, ` %`), `$`, ``, 1) + value, _ := strconv.ParseFloat(trimmed, 32) return float32(value) -} \ No newline at end of file +} + +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) +}