3

配列リストをソートしようとしています

例えば。

def list = [1, 1, 4, 4, 3, 4, 1]

ソートしたい:

[1, 1, 1, 4, 4, 4, 3]

どうもありがとうございました。


私は自分のコードに慣れている

例えば。

def plnProcessGoalInstance = ......someting    
def order = plnProcessGoalInstance.plnGoal.plnTargetPlan.id.unique() //[1, 4, 3,] ,plnProcessGoalInstance.plnGoal.plnTargetPlan.id = [1, 1, 4, 4, 3, 4, 1]
def plnProcessGoalInstance = plnProcessGoalInstance.sort{ a, b -> 
           order.indexOf(a.plnGoal.plnTargetPlan.id ) <=> order.indexOf(b.plnGoal.plnTargetPlan.id )}

助けてくれてどうもありがとう。

4

1 に答える 1

3

どうですか:

def order = [ 1, 4, 3 ]
def list = [ 1, 1, 4, 4, 3, 4, 1 ]

list.sort { a, b -> order.indexOf( a ) <=> order.indexOf( b ) }

assert list == [1, 1, 1, 4, 4, 4, 3]

または、Deruijter のコメントが正しいと仮定して、頻度の降順で並べ替え、次に同じ頻度の番号で並べ替えたいとします。

def list = [ 1, 1, 4, 4, 3, 4, 1 ]
def order = list.countBy { it }
                .sort { a, b -> 
                  b.value <=> a.value ?: a.key <=> b.key
                }.keySet().toList()
list.sort { a, b -> order.indexOf( a ) <=> order.indexOf( b ) }

countByGroovy 1.8 が必要

于 2012-11-21T13:31:17.560 に答える