myAlgorithm

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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值