标签:ima static 显示 接口 逻辑 log pre result alt
项目中通常有些需求为需要将某个sObject的数据列表按照某种规则排序显示到前台页面上,但是list上面的sort远远满足不了复杂的功能,此种情况需要自定义比较两个object大小的方法,所以需要创建Compare相关的类实现Comparable接口。
需求:实现Goods__c列表排序,GoodsBrand__c为华为的按照价格升序排序,GoodsBrand__c为联想的按照价格降序排列。
1 public without sharing class ComparedGoods implements Comparable{ 2 private Goods__c goods{get;set;} 3 4 private static final String GOODS_BRAND_HUAWEI = ‘华为‘; 5 6 private static final String GOODS_BRAND_LIANXIANG = ‘联想‘; 7 8 public ComparedGoods(Goods__c obj) { 9 goods = obj; 10 } 11 12 public Integer compareTo(Object objectToCompareTo) { 13 Goods__c comparedGoods = (Goods__c)objectToCompareTo; 14 Integer comparedResult = 0; 15 if(comparedGoods.GoodsBrand__c == GOODS_BRAND_HUAWEI) { 16 if(goods.GoodsPrice__c > comparedGoods.GoodsPrice__c) { 17 comparedResult = 1; 18 } else if(goods.GoodsPrice__c < comparedGoods.GoodsPrice__c) { 19 comparedResult = -1; 20 } 21 } else if(comparedGoods.GoodsBrand__c == GOODS_BRAND_LIANXIANG) { 22 if(goods.GoodsPrice__c > comparedGoods.GoodsPrice__c) { 23 comparedResult = -1; 24 } else if(goods.GoodsPrice__c == comparedGoods.GoodsPrice__c) { 25 comparedResult = 0; 26 } 27 else if(goods.GoodsPrice__c < comparedGoods.GoodsPrice__c) { 28 comparedResult = 1; 29 } 30 } 31 return comparedResult; 32 } 33 }
用法:
总结:针对object需要比较大小的时候,可以实现Comparable接口,在其compareTo方法中实现比较的逻辑即可。此篇有错误的地方欢迎指正,有问题欢迎留言。
salesforce 零基础学习(六十三)Comparable实现Object列表数据的自定义排序
标签:ima static 显示 接口 逻辑 log pre result alt
原文地址:http://www.cnblogs.com/zero-zyq/p/6278941.html