public class SortWrapper implements Comparable {
public CompleteRowWrapper wrapper;
// Constructor
public SortWrapper(CompleteRowWrapper wrapper) {
this.wrapper = wrapper;
}
// Compare opportunities based on the opportunity amount.
public Integer compareTo(Object compareTo) {
// Cast argument to SortWrapper
SortWrapper compareToWrapper = (SortWrapper)compareTo;
// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0;
if (wrapper.partialRowList.amountMap.get('Total') < compareToWrapper.wrapper.partialRowList.amountMap.get('Total')) {
// Set return value to a positive value.
returnValue = 1;
} else if (wrapper.partialRowList.amountMap.get('Total') > compareToWrapper.wrapper.partialRowList.amountMap.get('Total')) {
// Set return value to a negative value.
returnValue = -1;
}
return returnValue;
}
} // end the sortable
Take a look over this [1] blog post. It covers how to do it a little more dynamically. If you know that all of your fields can be sorted by using an integer comparison then you can set the field name as part of a static variable and then use your sObject.get method with that static var as the parameter.[1] http://blog.deadlypenguin.com/blog/2015/10/10/comparable-sorting-objects-in-salesforce/