0

I am using compareTo sort implementation. After sorting the wrapper I would like to get back a list of sObject. Is there a way to clone them off directly without hardcoding all field names?

Code:

   public class OpportunityWrapper implements Comparable {        
        public Electricity_Meter__c oppy;        
        public OpportunityWrapper(Electricity_Meter__c op) {
            oppy = op;
        }        
        public Integer compareTo(Object compareTo) {
            OpportunityWrapper compareToOppy = (OpportunityWrapper)compareTo;            
            Integer returnValue = 0;
            if (oppy.Meter_Details__c  > compareToOppy.oppy.Meter_Details__c ) {
                returnValue = 1;
            } else if (oppy.Meter_Details__c  < compareToOppy.oppy.Meter_Details__c ) {
                returnValue = -1;
            }            
            return returnValue;       
        }
    }
    public inputnulls(ApexPages.StandardSetController controller) { 
        mycon = controller; 
        Selectedmeters = mycon.getSelected();
        OpportunityWrapper[] oppyList = new List<OpportunityWrapper>();
        for (Electricity_Meter__c met: selectedmeters ){
            oppyList.add( new OpportunityWrapper(met ));            
        }
        oppyList.sort();

        for (OpportunityWrapper met: oppyList){
            selectedmeters2.add(new Electricity_Meter__c (met));                
        }                        
        selectedmeters = selectedmeters2;                                    
    } 

Currently I get SObject constructor must use name=value pairs error when adding sObjects back to collection.

Help please.

1 Answer 1

2

Just pull the record directly off the wrapper

public List<Electricity_Meter__c> getMeters() {
    OpportunityWrapper[] oppyList = new List<OpportunityWrapper>();
    // do stuff
    oppyList.sort();

    List<Electricity_Meter__c> selectedMeters = new List<Electricity_Meter__c>();
    for (OpportunityWrapper wrapper : oppyList){
        selectedmeters.add(wrapper.oppy);
    }
    return selectedMeters;
} 
4
  • Thanks! It worked. Was getting misleading error from console for a second, but it worked.
    – dzh
    Commented Jan 29, 2015 at 17:08
  • Too bad actual sort does not work.
    – dzh
    Commented Jan 29, 2015 at 17:21
  • Sounds like a different question. :)
    – Adrian Larson
    Commented Jan 29, 2015 at 17:22
  • It actually does, but it's VF where it gets randomised again :)
    – dzh
    Commented Jan 30, 2015 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .