データの整列
Posted feedbacks - Groovy
もともとgroovyには ソート用の拡張メソッドがいくつかあるので、 簡単にソートできます。 (いずれも1行でした。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // 準備
def output(comment, list){
println "■${comment}■"
def i = 1
list.each{
println "${i++}".padLeft(2) + " --- ${it}"
}
}
def list = []
for( i in 1..10 ){
list << [(int)(Math.random()*100), (int)(Math.random()*100)]
}
output("初期データ", list)
// 本題(2行だけw)
output("辞書順ソート", list.sort())
output("距離ソート", list.sort{ it[0]*it[0] + it[1]*it[1] })
|


odz #5839() Rating1/1=1.00
(x, y) の座標情報を以下の2種類の方法で整列する機能を実現してください。
データの表現方法はタプルなり構造体/オブジェクトなり各自で適当に選んで下さい。
[ reply ]