+ Start a Discussion
Deepak Pandey 13Deepak Pandey 13 

Display a greater record integer value from the list


Hi all,
      my requirment to find greatest integer in the list. In below code map have a size of integer. I want to take maximum integer value in the list(map1 contain integer list) and show on the page.

public class sizeorder 
{
public list<Ordered__c> lstorder {get;set;}

    public sizeorder()
    {
        lstorder = [select id,Name, Customer__c, Email__c from Ordered__c];
    map<string,list<Ordered__c>> map1 = new map<string,list<Ordered__c>>();
    
        for(Ordered__c ord : lstorder)
        {
            if(map1.get(ord.Customer__c )==null)
            {
             map1.put(ord.Customer__c ,  new list<Ordered__c >());
            }
           map1.get(ord.Customer__c).add(ord);
        }
     for(string str : map1.keyset())
     {
     list<Ordered__c> lst = map1.get(str);
     integer inte = lst.size();
     System.debug('@@@@@inte' + inte );
     integer i;
     integer j;
     integer k;
     integer l ;
     for(i=0; i<inte ; i++)
      {
     j = i++ ; 
     l= j ;
     system.debug('####j' +j); 
     system.debug('####l' +l); 
     system.debug('####i++' +i++); 
      }
      for (i = 1; i < inte ; i++)
      {
        if ( i > j)
        {
           j = k ; 
           system.debug('####j' +j); 
        }
      }
    }
  }
}
Dilip_VDilip_V
Hi Deepak,

Using sort method is the best option.
for(string str : map1.keyset())
     {
     list<Ordered__c> lst = map1.get(str);
     integer inte = lst.size();
    //Sort the list
     lst.sort();
     //Now the list is sorted in ascending order.
    //So the last element in the list is bigger one.
    //Get the element .
    LargeNum= lst.get(inte-1);

     }

For more info
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm

Thanks,
Dilip.
Magesh Mani YadavMagesh Mani Yadav
Hi Deepak,

If you want to show a table of sorted count of Ordered__c in desc order then this may help you.
List<AggregateResult> listOfOrdered = [select Customer__c,Count(Id) from Ordered__c group by Customer__c order by Count(Id) Desc];



 
Deepak Pandey 13Deepak Pandey 13
Hi Dilip,
     LargeNum= lst.get(inte-1); 
this function not working.
Dilip_VDilip_V
Deepak,

Try like this,
Integer LargeNum= lst.get(inte-1); 

If it didn't work plase share latest code.

Thanks.
Deepak Pandey 13Deepak Pandey 13
hello Dilip

public class sizeorder 
{
public list<Ordered__c> lstorder {get;set;}
public set<integer> inte {get;set;}

    public sizeorder()
    {
        lstorder = [select id,Name,Number__c , Customer__c, Email__c from Ordered__c];
    map<string,list<Ordered__c>> map1 = new map<string,list<Ordered__c>>();
    
        for(Ordered__c ord : lstorder)
        {
            if(map1.get(ord.Customer__c )==null)
            {
             map1.put(ord.Customer__c ,  new list<Ordered__c >());
            }
           map1.get(ord.Customer__c).add(ord);
        }
     for(string str : map1.keyset())
     {
     list<Ordered__c> lst = map1.get(str);
     lst.sort();
     integer inte = lst.size();
      System.debug('@@@@@inte' + inte );
     System.debug('!!!!!!!!!!!lst' + lst);   
      }   
    }
}


  integer LargeNum= lst.get(inte-1);
lst use ordered__c so it i use  than so error illegal assaign in ordered__c...  
 
Dilip_VDilip_V
Deepak,
Have you tried like this.
integer LargeNum= lst.get(inte-1).Number__c;

 
Deepak Pandey 13Deepak Pandey 13
hi Dilip
null value return.