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 

How to sort a list of records with respect to one DATetime field


I have a list of records and now i have a requirement that  the list of records should get sorted according the date field in the inner query .

list<contact> conmap=new list<contact>([SELECT Id, (SELECT Status,Subject,ActivityDate FROM Tasks order by ActivityDate ASC) FROM Contact where id=:conid]);


Now in list "conmap ": i want tasks records to get sorted according to the activitydate field , which means the activitydate field record of  min value  belonging to record should come at 0th index of   the new list....



This cant be directly done using SOQL.
You may have to iterate through it to sort the list or you can have a look at comparable class to implement your own sorting using wrapper class

seriously I have no idea about compare class ..If u can pls elaborate more on the soql process.


Because as u can see in the above query that the comparison occurs but it occurs in such a way that tasks under 1st contact get ORDER BY then  tasks of 2nd contact GEt ORDER by , But what i want is to get a list in which i get the all tasks of  all contacts under one account get sorted at once . 


FYI :Account is not directly related to tasks therefore going through contacts of the account..

 I hope u got it..


THank u

You are querying all the task related to contact ?

So do this
List<Task> tasks = [SELECT Status,Subject,ActivityDate FROM Tasks order by ActivityDate ASC WHERE WhoId=:con.Id]