function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AkiraDioAkiraDio 

Sort List or Map

Good day! I have two questions:
1. How to sort List?
2. How to sort the Map?
Sorting must take place not in the query.
Example:
Map <id,Action_Goal__c> aMap = new Map <id,Action_Goal__c> (
                                  [select Goal_Description__c, id, Goal_Type__c, Targeted_Due_Date__c, Name, Status__c
                                     From Action_Goal__c
                                     Where ToLabel (Goal_Type__c) =: MyGoalType]);

  List <Action_Goal__c> MyActGoalsDescList = aMap.values ();

I need to sort by Targeted_Due_Date__c.

liron169liron169

The List object have sorting function, but I assume it won't help in case the type in the list is Object.

So I assume you will need to write your own sort function for this issue.

 

 

AkiraDioAkiraDio
(((If I knew how, then there would not be writing (
liron169liron169

Maybe try something like...


List<myObject> sortedList=new List<myObject>();

while(originalList.size()>0)
{
    myObject nextObj=originalList.get(0);
    Integer nextObjIndex=0;

    for(Integer index=0; index<originalList.size();index++)
    {
        if(tempObject.dateField < nextObj.dateField)
        {
            nextObj=tempObject;
            nextObjIndex=index;
        }
    }

    sortedList.add(originalList.remove(nextObjIndex));
}