Groovy

//字符串部分值排序
def valueList = ['12035-52-16152','11265-84-18452','12005-65-18452']
valueList.sort {x, y->
    def xlist = []
    def yList = []
    xlist = x.split('-').toList() - ['']
    yList = y.split('-').toList() - ['']
    return xlist[0].toInteger() - yList[0].toInteger()
}
println valueList //Result: [11265-84-18452, 12005-65-18452, 12035-52-16152]
//以上如果没有拆开字符比较的必要,可以直接比较
valueList.sort {x, y->
    x.compareTo(y)
}
println valueList //Result: [11265-84-18452, 12005-65-18452, 12035-52-16152]
//升序
def valueList = [15,10,8,51,36]
valueList.sort{x,y->
    return x - y
}
println valueList  //Result: [8, 10, 15, 36, 51]
//降序(只须将y和x换位)
def valueList = [15,10,8,51,36]
valueList.sort{x,y->
    return y - x
}
println valueList //Result: [51, 36, 15, 10, 8]
//字符串排序
def strList = ['abc','aab','bac']
strList.sort{x,y->
  x.compareTo(y)
}
println strList //Result: [aab, abc, bac]

你可能感兴趣的:(groovy)