//将Map键值对按值降序排序,返回指定个键值对
static def sortor(map, resultLength) {
def resultList = []
if (!map.isEmpty()) {
def vals = map.values().toArray()
def keys = map.keySet().toArray()
for (int i=0; i < (vals.size() - 1); i++) {
for (int j=(i + 1); j < vals.size(); j++) {
if (vals[i] < vals[j]) {
//值按降序排序
def tempValue = vals[i]
vals[i] = vals[j]
vals[j] = tempValue
//键和值同步排序
def tempKey = keys[i]
keys[i] = keys[j]
keys[j] = tempKey
}
}
}
def resultKeys = []
def resultVals = []
for (int i=0; i < vals.size(); i++) {
resultKeys << keys[i]
resultVals << vals[i]
}
if (resultLength > vals.size()) {
resultList << resultKeys
resultList << resultVals
} else {
resultList << resultKeys.subList(0,resultLength)
resultList << resultVals.subList(0,resultLength)
}
}
return resultList
}
static def sortor(map, resultLength) {
def resultList = []
if (!map.isEmpty()) {
def vals = map.values().toArray()
def keys = map.keySet().toArray()
for (int i=0; i < (vals.size() - 1); i++) {
for (int j=(i + 1); j < vals.size(); j++) {
if (vals[i] < vals[j]) {
//值按降序排序
def tempValue = vals[i]
vals[i] = vals[j]
vals[j] = tempValue
//键和值同步排序
def tempKey = keys[i]
keys[i] = keys[j]
keys[j] = tempKey
}
}
}
def resultKeys = []
def resultVals = []
for (int i=0; i < vals.size(); i++) {
resultKeys << keys[i]
resultVals << vals[i]
}
if (resultLength > vals.size()) {
resultList << resultKeys
resultList << resultVals
} else {
resultList << resultKeys.subList(0,resultLength)
resultList << resultVals.subList(0,resultLength)
}
}
return resultList
}