Salesforce中Comparable比较Object的列表数据自定义排序

项目中通常有些需求为需要将某个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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值