Sorting 如何对文本列表进行排序+;Groovy中的日期字符串

Sorting 如何对文本列表进行排序+;Groovy中的日期字符串,sorting,groovy,Sorting,Groovy,我有一个字符串列表,每个字符串都包含带有日期的文本,如下所示: "foo_6.7.2016" "foo_5.10.2016" "foo_6.30.2016" "foo_6.23.2016" "foo_6.2.2016" "foo_5.22.2016" 我需要按日期对它们进行排序,并获得以下信息: "foo_6.30.2016" "foo_6.23.2016" "foo_6.7.2016" "foo_6.2.2016" "foo_5.22.2016" "foo_5.10.2016" 对于需要大

我有一个字符串列表,每个字符串都包含带有日期的文本,如下所示:

"foo_6.7.2016"
"foo_5.10.2016"
"foo_6.30.2016"
"foo_6.23.2016"
"foo_6.2.2016"
"foo_5.22.2016"
我需要按日期对它们进行排序,并获得以下信息:

"foo_6.30.2016"
"foo_6.23.2016"
"foo_6.7.2016"
"foo_6.2.2016"
"foo_5.22.2016"
"foo_5.10.2016"

对于需要大量清理的快速答案:

def dates = [
"foo_6.7.2016"
"foo_5.10.2016"
"foo_6.30.2016"
"foo_6.23.2016"
"foo_6.2.2016"
"foo_5.22.2016"
]

def prefix = "foo_"
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M.d.yyyy")
def sorted_dates = dates.collect{ sdf.parse(
    it, new java.text.ParsePosition(prefix.length()) ) }.sort().reverse()
def newDates = sorted_dates.collect{ "${prefix} + ${sdf.format(it)}"}
println newDates

另一种选择可能是:

def foos = [ 
    "foo_6.7.2016",
    "foo_5.10.2016",
    "foo_6.30.2016",
    "foo_6.23.2016",
    "foo_6.2.2016",
    "foo_5.22.2016"
]

def sorted = foos.sort(false) { Date.parse('M.d.yyyy', it - 'foo_') }.reverse() 

很好-肯定是一种比我的方法更为常规的方法。你的方法的优点是,你只需要在使用spaceship操作符时解析日期。。。那么reverse()可以消失了吗?假设f是这个答案中指定的闭包。非常感谢!正是我需要的。谢谢,这对我很有用!