Skip to content

Instantly share code, notes, and snippets.

@c-yan
Last active February 27, 2018 13:44
Show Gist options
  • Save c-yan/14d765b92ee95e49d4b7621a9b1c4d87 to your computer and use it in GitHub Desktop.
Save c-yan/14d765b92ee95e49d4b7621a9b1c4d87 to your computer and use it in GitHub Desktop.
18. 各行を3コラム目の数値の降順にソート 各行を3コラム目の数値の逆順で整列せよ(注意: 各行の内容は変更せずに並び替えよ).確認にはsortコマンドを用いよ(この問題はコマンドで実行した時の結果と合わなくてもよい).
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
func getSortKey(s string) float64 {
result, err := strconv.ParseFloat(strings.Split(s, "\t")[2], 64)
if err != nil {
panic(err)
}
return result
}
func main() {
buffer := make([]string, 0)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
buffer = append(buffer, scanner.Text())
}
sort.Slice(buffer, func(i, j int) bool { return !(getSortKey(buffer[i]) < getSortKey(buffer[j])) })
for _, line := range buffer {
fmt.Println(line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment