+ Start a Discussion
inbox outbox 7inbox outbox 7 

I want to reverse an SObject list using comparible interface. Can anyone help me with that?

public class IntWrapper implements Comparable{
    public Integer int;
    public static String order = 'ASC';
    
    public IntWrapper(Integer val){
        int = val;
    }

    public Integer compareTo(Object input){
        Integer result;
        if(IntWrapper.order == 'ASC'){
            result = compareASC(input);
        } else {
            result = compareDESC(input);
        }
        return result;
    }

    private Integer compareASC(Object input){
        Integer intIn = (Integer)input;
        if(int < intIn){
            return -1;
        } else if(int > intIn){
            return 1;
        } else {
            return 0;
        }
    }

    private Integer compareDESC(Object input){
        Integer intIn = (Integer)input;
        if(int < intIn){
            return 1;
        } else if(int > intIn){
            return -1;
        } else {
            return 0;
        }
    }
}

List<IntWrapper> someList = new List<IntWrapper>{new IntWrapper(1), new IntWrapper(2), new IntWrapper(3), new IntWrapper(4), new IntWrapper(5)};

// First sort ensures we're in ASC order
someList.sort();

// Set the static flag, sort again, et voila, list is reversed
IntWrapper.order = 'DESC';
someList.sort();

I want to be able to reverse a list of accounts using this approach.