-
-
Save horlabyc/302c48fdb89bbb9d75801db0d48f88d0 to your computer and use it in GitHub Desktop.
Map and sort data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"log" | |
"os" | |
"sort" | |
"strconv" | |
) | |
type Data struct { | |
Key string | |
Value float64 | |
} | |
type DataList []Data | |
func (d DataList) Len() int { | |
return len(d) | |
} | |
func (d DataList) Swap(i, j int) { | |
d[i], d[j] = d[j], d[i] | |
} | |
func (d DataList) Less(i, j int) bool { | |
return d[i].Value < d[j].Value | |
} | |
func mapData(gameNames []string, sales []float64) DataList { | |
dataMap := map[string]float64{} | |
for index, value := range gameNames { | |
dataMap[value] = sales[index] | |
} | |
data := make(DataList, len(dataMap)) | |
iterator := 0 | |
for k, v := range dataMap { | |
data[iterator] = Data{k, v} | |
iterator++ | |
} | |
sort.Sort(sort.Reverse(data)) | |
return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment