+ Start a Discussion
Harpreet Kaur 45Harpreet Kaur 45 

How to compare one filed of objects in list of objects and based on that only display the different values

for example , List<Class__c> c = [Select CurrentRates__c,.... from Class__c];
                      List<List<Class__c>> abc = new List<List<Class__c>>();
                      abc.add(c);  // Suppose we added 3 Classes. Could be more
So, Based on CurrentRates field I want to compare all the classes in abc and and if CurrentRates field value is different then, I want to add all other fields. This what i have in my List of Objects. I ant to comapre for example Life current rates and combine them. if same only display onceand if different then display both in rows and add the values.
Please help
User-added image
Best Answer chosen by Harpreet Kaur 45
Malni Chandrasekaran 2Malni Chandrasekaran 2
Harpreet,
Still I dont see the need of having List<List<class__c>>structure when you use a single query to retrieve the records and all the records in same structure. Please clarify if my understanding has a gap.
If you have to compare one record with other records, here is the logic (Not complete code)

for(integer i = 0; i < ClassSize; i++)
  for (integer j = i+1;  j<ClassSize; j++) // J initial value is i+1 - this is to avoid duplicate checks
{
If( Classinfo[i].currentrate == classinfo[j].currentrate)  // Assume classinfo is the list of object class__c
{
do your logic... as the j value increments first record will be compared with all the other records... when i value increments to 1, second record will be compared with all the other records from 3rd till end. and so on...
}
}


Hope this helps!

 

All Answers

Malni Chandrasekaran 2Malni Chandrasekaran 2
Harpreet,
Sorry your question is not very clear.
- do you have different objects (like Class A, Class B, Class C) with the same field name current_rates__C?
- what do you have to compare with?
can you please explain your requirement with some example? 
 
Harpreet Kaur 45Harpreet Kaur 45
I have one object named Class__c and I have different instances of that object that iam getting from SQL query. So get 3 different records named Class a, Class b, Class c. All classes have same fields.im picture of table above I am  displaying 2 classes each having fields as headings in my table
Harpreet Kaur 45Harpreet Kaur 45
I want to compare current rates for Life, AD&D, CI,STD,LTD from one record /Class a to current rates for Life, AD&D, CI,STD,LTD from 2nd record/Class b
Harpreet Kaur 45Harpreet Kaur 45
Here code bit of code I have
if(ClassSize > 0) {
      classInfo = new List<List<Class__c>>();
      for(Integer i=0; i<ClassSize; i++)
      { 
         List<Class__c> ClassRates = new List<Class__c>();
         ClassRates = getPricingSheetInfo(i);  // This fucntion has SQL query.It returns rates for each class.
         if(ClassRates[0].Title_for_Rate_Sheet__c != NULL)
         {
         classInfo.add(ClassRates);           
         }   
}
So in classInfo, I have 3 classes with rates. I want to compare only current rates for 3 classes. Suppose if current rates for 1st 2 classes are same then i want to display only i record and if current rates are different then display both and add the rates 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Harpreet,
Still I dont see the need of having List<List<class__c>>structure when you use a single query to retrieve the records and all the records in same structure. Please clarify if my understanding has a gap.
If you have to compare one record with other records, here is the logic (Not complete code)

for(integer i = 0; i < ClassSize; i++)
  for (integer j = i+1;  j<ClassSize; j++) // J initial value is i+1 - this is to avoid duplicate checks
{
If( Classinfo[i].currentrate == classinfo[j].currentrate)  // Assume classinfo is the list of object class__c
{
do your logic... as the j value increments first record will be compared with all the other records... when i value increments to 1, second record will be compared with all the other records from 3rd till end. and so on...
}
}


Hope this helps!

 
This was selected as the best answer
Harpreet Kaur 45Harpreet Kaur 45
Number of classes is not fixed. that's why I am looping to retrive class rates with function getPricingSheetInfo(i).  and Using List<List<Class__c>> .For example if account has  9 classes, then i would need 9 variables to hold 9 classes. yes you code is really helpful. thank you 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Glad it helped you :) please mark it as solved.