Groovy提前排序 [英] Groovy advance sort
本文介绍了Groovy提前排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组列表:
I got an array list:
def a = ['Application-11.pdf','Application-11-01-29-15-03-55.pdf','Application-11-02-29-15-03-55.pdf ']
print a.sort() == [Application-11-01-29-15-03-55.pdf, Application-11-02-29-15-03-55.pdf , Application-11.pdf]
所以,我想分类:
So, I wanna sort by:
['Application-11.pdf', 'Application-11-01-29-15-03-55.pdf', Application-11-02-29-15-03-55.pdf]
有没有可能的解决方案。
Is there any possible solution for this.
谢谢。
推荐答案
这:
You could do a custom sort closure like this:
def list = [
'Application-11.pdf',
'Application-11-01-29-15-03-55.pdf',
'Application-11-03-29-15-03-55.pdf',
'Application-11-02-29-15-03-55.pdf'
]
def sorter = { String a, String b ->
a.length() <=> b.length() ?: a <=> b
}
println list.sort(false, sorter)
这篇关于Groovy提前排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文