0

I have List of Records for which I want to find out Maximum value for particular Field:

Can anyone suggest me, any efficient way (rather than writing Sorting Method)

Example :

public decimal maxField1;
public decimal maxField2;

.....
{

List<MyObject__c> listMO = [SELECT ID, Team__c,Field1__c,Field2__c FROM  MyObject__c WHERE xyz__c='abc'];

//Assume I have Map of Teams like below (Not used code here)
MyMap { (Team1,<MyObject1, MyObject2>),
        (Team2,<MyObject3, MyObject4>)
      }
//calling myMethod to get maximum
for(ID key :MyMap.keyset()){
       myMethod(MyMap.get(key));
}
}
...
..
//Find maximum of Field1, field2 for each Team

//---right now I am using SOQL - which I dont want to use

public void myMethod(List<MyObject__c> lstParameter){

 maxField1 = [SELECT Max(field1__c) FROM MyObject__c WHERE ID IN: lstParameter];
}

HERE : I dont want to use SOQL's anymore ....... is there any way that I can find out "Maximum of 'Field Value' for only given List of records at movement.

Any Suggestions! Help !

EDITS :

"Comparable Interface" is the one way - can anyone explain it with simplest real time example (Not Salesforce HelpPage)

  • 1
    Can you try to just go for given list , and if you see bigger value that already stored -- save it? – kurunve May 12 '16 at 10:15
  • Thats the universal way, but lengthy - which I'm trying to avoid. (e.g. Binary Sort) – Salesforce Steps May 12 '16 at 12:43
  • unfortunately, there are no faster way. On array of unsorted data, search of max/min element would always take O(n). I am still not sure why that is long for you. even if you use 50k of records, and that haven't eaten your heap limit -- that would work – kurunve May 12 '16 at 13:59
  • 1
    Here is an example of using comparable. – Keith C May 13 '16 at 9:49
2

You can update your code as

//calling myMethod to get maximum
Integer dummy = 0;
for(ID key :MyMap.keyset()){

   // Here you can check if the current field value is greater or not if greater will update the dummy var with new value  
   if(dummy < MyMap.get(key).field1__c)
       dummy = MyMap.get(key).field1__c;

  // myMethod(MyMap.get(key));
}

Hope this help you out. If you want to know which record have the max value then have a map with Id and integer.

  • I have to find out Max of 4 different fields for each record, so instead of writing for loop and doing it, is there any shortCut OR Efficient way to get it done ? – Salesforce Steps May 12 '16 at 10:37
1

You can follow the below process:

  1. Create 4 maps of Map
  2. Move the keyset from Map 1 to List 1. You can do this the addAll function of list which takes a set as a parameter
  3. Sort the list.
  4. Take the last value in the list. That would be the max value.
  5. Fetch the object from the map using this value.

You can create a static method which can take the map as a parameter and return the largest MyObject. Also, note that it might be possible that 2 or more MyObject records can have the same decimal value. If you only one of them, the above approach will work fine. If you want all, the map needs to be updated as Map>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.