# salesforce 零基础学习（六十三）Comparable实现Object列表数据的自定义排序

需求：实现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 条评论

## 相关文章

2894

### Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2906

### Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显，操作也非常简便，强烈推荐各位在集群上线之前进行这一操作，能够极大的提升整个集群...

3645

5218

### sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2820

82111

### cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

6976

### The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

38210

### Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2767

1.4K7