如何在groovy中对列表进行排序

如何在groovy中对列表进行排序,groovy,Groovy,我正在尝试对数组列表进行排序 例如 希望分类: [1, 1, 1, 4, 4, 4, 3] 多谢各位 我习惯了我的代码 例如 def plnProcessGoalInstance=…一些 def order=plnProcessGoalInstance.plnGoal.plnTargetPlan.id.unique()/[1,4,3,3],plnProcessGoalInstance.plnGoal.plnTargetPlan.id=[1,1,4,4,3,4,1] def plnProces

我正在尝试对数组列表进行排序

例如

希望分类:

[1, 1, 1, 4, 4, 4, 3]
多谢各位


我习惯了我的代码

例如

def plnProcessGoalInstance=…一些
def order=plnProcessGoalInstance.plnGoal.plnTargetPlan.id.unique()/[1,4,3,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)}
非常感谢您的帮助。

怎么样:

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]
def顺序=[1,4,3]
def列表=[1,1,4,4,3,4,1]
list.sort{a,b->order.indexOf(a)order.indexOf(b)}
断言列表==[1,1,1,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 ) }
def list=[1,1,4,4,3,4,1]
def order=list.countBy{it}
.sort{a,b->
b、 值a.值?:a.键b.键
}.keySet().toList()
list.sort{a,b->order.indexOf(a)order.indexOf(b)}

countBy
需要Groovy 1.8

是否要在末尾用3对其排序?你能描述一下你的排序是如何工作的吗?看起来你想根据:A.某些数字的出现(降序)B.数字(升序)[1,2,3,4,1]->[1,1,2,3,4][1,4,3,2,1]->[1,1,4,3,2,1]->[1,4,3,2]这是一样的groupBy@Deruijter猜得好。。。看来你是对的
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]
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 ) }