myAlgorithm_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > myAlgorithm

myAlgorithm

 2012/6/29 16:37:12  robinfung  程序员俱乐部  我要评论(0)
  • 摘要:1.MultiplesortingHowJava?referencethishttp://stackoverflow.com/questions/1421322/how-do-i-sort-a-list-with-multiple-sort-parametersHowGroovy?onlyonerecursive+dynamiccaninstandtheenumclass(orswitchstatement).classClazz
  • 标签:

1. Multiple sorting

How Java? reference this

http://stackoverflow.com/questions/1421322/how-do-i-sort-a-list-with-multiple-sort-parameters

?

How?Groovy? only one recursive + dynamic can instand the enum class(or switch statement).

?

class Clazz {
    String name
    String uid
    String firstName
    String lastName
}

Clazz c = new Clazz(name : "Andy", uid :1, firstName: 1, lastName: 1)
Clazz c1 = new Clazz (name: "Andy", uid: 2, firstName: 1, lastName: 1)
Clazz c2 = new Clazz (name: "Andy", uid: 2, firstName: 2, lastName: 1)
Clazz c3 = new Clazz (name: "Andy", uid: 2, firstName: 2, lastName: 2)
def objects = [c,c1,c2,c3]
def properties = ["name"]
sorting(objects, properties).each {
    println "============="
    println it.name+" "+it.uid+" "+it.firstName+" "+it.lastName
}

protected def sorting(List<?> items, List properties){   
    items.sort {i1, i2-> 
        println "---------------------"
        println i1.name+" "+i1.uid+" "+i1.firstName+" "+i1.lastName
        println i2.name+" "+i2.uid+" "+i2.firstName+" "+i2.lastName    
        compare(i1, i2, properties, 0)    
    } 
}   


private def compare(i1, i2, properties, i){
    def flag = i1."${properties[i]}" <=> i2."${properties[i]}"
    if(flag == 0 && properties.size()>1){
        compare(i1, i2, properties, i+1)
    } else {
        return flag
    }
}

  • 相关文章
发表评论
用户名: 匿名