Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package main
import (
"fmt"
"time"
"sort"
"math/rand"
)
func sortInt() {
rand.Seed(time.Now().UTC().UnixNano())
count := 30
maxInt := 10000
arr := make([]int , count)
for i, _ := range arr {
arr[i] = rand.Int() % int(maxInt)
}
fmt.Println("Sort this array : ", arr)
sort.Sort(sort.IntSlice(arr))
fmt.Println("Sorted result : ", arr)
sort.Sort(sort.Reverse(sort.IntSlice(arr)))
fmt.Println("Reversed result : ", arr)
}
func sortRankings(){
rankings := []Ranking {
{"88154420822432544", 1436520192, 13960},
{"91670176688136577", 1440799941, 13080},
{"88180751521592112", 1435637878, 14280},
{"88144324860977520", 1437278775, 13560},
{"93797540915256451", 1435889873, 14200},
{"88249875880704624", 1438226613, 11960},
{"88192601275706464", 1436694190, 13320},
{"88148115536237952", 1436450939, 13600},
{"89047353485705664", 1435654249, 12760},
{"88182263314045056", 1436586635, 13320},
{"88193534078563601", 1438176365, 16600},
{"88256711722920353", 1437136853, 8920},
{"89852877819675984", 1436684270, 12680},
{"93594773313085234", 1437634370, 8480},
{"90289273133727025", 1436351481, 10800},
{"92150085612088915", 1440609977, 12720},
{"91211930152797040", 1438076721, 10920},
{"88309448624594448", 1438057506, 10440},
{"88406950494717233", 1436508922, 11000},
{"93176928270204371", 1437293064, 10440},
{"88056851903088048", 1436338570, 10440},
{"93691000129391522", 1436618165, 12560},
{"88675559501659840", 1438230885, 9800},
}
sort.Sort(RankingSlice(rankings))
fmt.Println(rankings)
}
// Ranking struct
type Ranking struct {
kakaoid string
written_date int
score int
}
func (ranking Ranking) String () string {
return fmt.Sprintf("kakaoid %s score %d written_date %d\n", ranking.kakaoid, ranking.score, ranking.written_date)
}
// RankingSlice----------------------------------------------START
type RankingSlice []Ranking
func (arr RankingSlice) Len() int {
return len(arr)
}
// j 인덱스를 가진녀석이 i 앞으로 와야하는지 말아야하는지를 판단하는 함수
func (arr RankingSlice) Less(i, j int) bool {
if arr[i].score == arr[j].score {
return arr[i].written_date > arr[j].written_date
} else {
return arr[i].score > arr[j].score
}
}
func (arr RankingSlice) Swap(i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}
//RankingSlice----------------------------------------------END
func main() {
sortInt()
sortRankings()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment