genzou #6472(2008/06/12 14:58 GMT) [ Groovy ] Rating0/0=0.00
もともと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] })
Rating0/0=0.00-0+
[ reply ]
genzou
#6472()
[
Groovy
]
Rating0/0=0.00
もともとgroovyには ソート用の拡張メソッドがいくつかあるので、 簡単にソートできます。 (いずれも1行でした。)
// 準備 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] })Rating0/0=0.00-0+
[ reply ]