My favorites | Sign in
Project Home Downloads Wiki Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
global class SObjectSortByFieldComparator implements ISObjectComparator{

//==================================================
// PROPERTIES
//==================================================
private static final PrimitiveComparator primitiveComparator = new PrimitiveComparator();

global String sortBy {get;set;}
global Boolean sortAscending {get;set;}

//==================================================
// CONSTRUCTOR
//==================================================
global SObjectSortByFieldComparator(
String sortBy
){
this(sortBy,null);
}

global SObjectSortByFieldComparator(
String sortBy
,Boolean sortAscending
){
this.sortBy = sortBy;
this.sortAscending = sortAscending;
}

//==================================================
// METHODS
//==================================================
global Integer compare(SObject object1, SObject object2){
if(object1==null && object2 == null){
return 0;
} else if(object1==null){
return -1;
} else if(object2==null){
return 1;
}
if(StringUtils.isBlank(sortBy)) throw new IllegalStateException('sortBy cannot be blank');
Integer returnValue = primitiveComparator.compare(
object1.get(sortBy)
,object2.get(sortBy)
);
if(sortAscending != null && !sortAscending){
if(returnValue == -1){
returnValue = 1;
} else if(returnValue == 1){
returnValue = -1;
}
}
return returnValue;
}

global static List<SObject> qsort(List<SObject> objects){
return qsort(objects,null,null);
}

global static List<SObject> qsort(List<SObject> objects, String sortBy){
return qsort(objects,sortBy,null);
}

global static List<SObject> qsort(List<SObject> objects, Boolean sortAscending){
return qsort(objects,null,sortAscending);
}

global static List<SObject> qsort(List<SObject> objects, String sortBy, Boolean sortAscending){
if(StringUtils.isBlank(sortBy)) sortBy = 'name';
if(sortAscending == null) sortAscending = true;
return qsort(objects,new SObjectSortByFieldComparator(sortBy,sortAscending));
}

global static List<SObject> qsort(List<SObject> objects, SObjectSortByFieldComparator comparator){
return ArrayUtils.qsort(objects,comparator);
}
}

Change log

r118 by Richard.Vanhook on Apr 13, 2011   Diff
1.17 unit test updates
Go to: 
Project members, sign in to write a code review

Older revisions

r117 by Richard.Vanhook on Apr 13, 2011   Diff
Updates
All revisions of this file

File info

Size: 3011 bytes, 86 lines
Powered by Google Project Hosting