3

In my apex code I am adding some value into map. And I am trying to loop through the map on VF page using apex:repeat but I am getting an error that

This map cannot be used in an iteration because the keys cannot be sorted

Is anyone here can tell me what might be the issue. The versions of both the Apex code and VF page is same 31. my code is bellow

VF page :

    <apex:repeat value="{!sObjectFieldMap}" var="sobj">

    </apex:repeat>

controller :

public with sharing class survey_ResultV2Ctrl{

    public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public Map<Schema.SObjectType, List<Schema.SObjectField>> sObjectFieldMap {get; set;}
    public List<Schema.SObjectField> sObjectFieldLst = new List<Schema.SObjectField>();
    public List<wrapperClass> wcls {get; set;}
    public set<Schema.SObjectType> objNameSet =  new set<Schema.SObjectType>();

    public survey_ResultV2Ctrl(){
        sObjectFieldMap = new  Map<Schema.SObjectType, List<Schema.SObjectField>>();
        wcls =  new List<wrapperClass>();
        survey_ResultDisplay();
    }

    public void survey_ResultDisplay(){

        for(Filter_Field_Set__c ffs : [SELECT Name, Field_Set_Category__c, Object_Fields__c, Object_Fields_Name__c FROM Filter_Field_Set__c Where Field_Set_Category__c = 'a0F9000000R5Fau']){
            system.debug('All the result from Filter Field Set object ' + ffs.Name);
            system.debug('Schema Map contains Key as object name ' + schemaMap.containsKey(ffs.Name));
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(ffs.Name).getDescribe().fields.getMap();
            if(schemaMap.containsKey(ffs.Name)){
                system.debug('Schema object Values ' + schemaMap.get(ffs.Name));
                system.debug('All the fields related to Schema object ' + fieldMap.get(ffs.Object_Fields_Name__c));

                if(sObjectFieldMap.containskey(schemaMap.get(ffs.Name))){
                    sObjectFieldLst.add(fieldMap.get(ffs.Object_Fields_Name__c));
                    sObjectFieldMap.put(schemaMap.get(ffs.Name), sObjectFieldLst.clone());
                    //objNameSet.add(schemaMap.get(ffs.Name));
                    //wcls.add(new wrapperClass(schemaMap.get(ffs.Name), sObjectFieldLst));
                    sObjectFieldLst.clear();
                }
                else{
                    sObjectFieldLst.add(fieldMap.get(ffs.Object_Fields_Name__c));
                    sObjectFieldMap.put(schemaMap.get(ffs.Name), sObjectFieldLst.clone());
                    //wcls.add(new wrapperClass(schemaMap.get(ffs.Name), sObjectFieldLst));
                    //objNameSet.add(schemaMap.get(ffs.Name));
                }
            }

        }


        system.debug('The wapper class after loop ' + wcls);
        system.debug('The Map after loop ' + sObjectFieldMap);

    }


    public with sharing class wrapperClass{

        public list<Schema.SObjectField> fieldname {get; set;}
        public Schema.SObjectType objectName {get; set;}

        public wrapperClass(Schema.SObjectType objectName, list<Schema.SObjectField> fieldname){
             objectName = objectName;
             fieldname = fieldname;
        }
    }    
}

Please help me if anyone have any idea on this issue.

2
  • 3
    Even though it's allowed, try not to use complex objects as Map keys. Instead of using the SObjectType, get the type's object name, which is a unique String, and use that as the key.
    – ScottW
    Commented Sep 27, 2014 at 17:32
  • It is perfectly safe to use complex objects as keys, as long as they are implementing hashCode() and equals(). This might not be true for Schema.SObjectType and result in the error mentioned. Commented Sep 29, 2014 at 10:12

1 Answer 1

1

Instead of using complex objects as keys for the map, you can use wrapper class and comparable interface for sorting the values.

For Salesforce Documentation: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_comparable.htm

You must log in to answer this question.

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