zoukankan      html  css  js  c++  java
  • 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 }

    用法:

    总结:针对object需要比较大小的时候,可以实现Comparable接口,在其compareTo方法中实现比较的逻辑即可。此篇有错误的地方欢迎指正,有问题欢迎留言。

  • 相关阅读:
    html设置360兼容/极速模式
    es查询备忘录
    TypeError: __init__() got an unexpected keyword argument 'strict'
    pandas处理csv
    scrapy中cookie处理
    滑动验证码破解(一)
    python 自定义属性访问 __setattr__, __getattr__,__getattribute__, __call__
    数据库的增删改、单表查询
    库的操作,表的操作,数据类型,完整性约束
    Mysql常用命令(二)
  • 原文地址:https://www.cnblogs.com/zero-zyq/p/6278941.html
Copyright © 2011-2022 走看看