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
pallipalli 

how to display list of values in Descending order

Hi,  how to display list of values in Descending order .

 

apex Class :

============

public class SetPrimitiveDatatype
{
public set<String> fstName{get;set;}
public SetPrimitiveDatatype()
{
fstName=New set<String>();

fstName.add('Rama');
fstName.add('Anil');
fstName.add('Srinu');
fstName.add('Kumar');
fstName.add('Prasad');
fstName.add('Devaraju');
fstName.add('Murali');
fstName.add('Balu');
fstName.add('Chaitu');
fstName.add('Tanuja');
}
}

 

page

============

 

<apex:page controller="SetPrimitiveDatatype">
<apex:form >
<apex:pageBlock title="Display The Set of values">
<apex:pageBlockSection title="FirstName">
<apex:pageblockTable value="{!fstName}" var="fn">
<apex:column value="{!fn}"/>
</apex:pageblockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

Please help me friends.............

Best Answer chosen by Admin (Salesforce Developers) 
SammyComesHereSammyComesHere
List<String> sampleList =new List<String>();
sampleList.add('Andrew');
sampleList.add('Sammy');
sampleList.add('Yogut');
sampleList.sort();  // This function always results in asc order.


List<String> finalList = new List<String>();
for(Integer i = sampleList.size()-1; i>=0;i--)
{
    finalList.add(sampleList.get(i));
}

System.debug('Check the Order -->'+finalList); // This should be in descending order

 

 

All Answers

ManjunathManjunath

Hi!

 

Instead of using set use list. Once you have added the entries into the list call any std sort method. This would solve that.

 

Regards,

Manjunath

 

SammyComesHereSammyComesHere

Not pretty  sure whether you can do it like that. You might need to implement Comparable interface for this purpose.Please find the Link .http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_lists.htm#list_sobject_sorting

 

May be if it is a mandatory requirement then make use of Custom Setting and then Query Custom Setting objects with order By Column Name desc.

 

This should definetely Work.

pallipalli

i am a bigineer , i am solving sort , but i want to descending order list of values ........

 

in my code where i am modifing and adding the code methods...

 

please help me ..........

 

thanks

ManjunathManjunath

Hi !!!

 

You can create your own sort method. Once you have filled the data into list, call this function. Use your normal bubble sort.

 

 

 

Regards 

Manjunath

 

pallipalli

Hi,

 

i dnt know how to create own sort method. 

 

plz u create sort method and send me the code  ...........

please help me.....

pallipalli

Hi,

i am here stuck ...........

 

plz help me.......

SammyComesHereSammyComesHere

May be if it is a mandatory requirement then make use of Custom Setting and then Query Custom Setting objects with order By Column Name desc.

 

This should definetely Work.

pallipalli

hi,

please help me ,

 

for loop concept using descending order.................

SammyComesHereSammyComesHere
List<String> sampleList =new List<String>();
sampleList.add('Andrew');
sampleList.add('Sammy');
sampleList.add('Yogut');
sampleList.sort();  // This function always results in asc order.


List<String> finalList = new List<String>();
for(Integer i = sampleList.size()-1; i>=0;i--)
{
    finalList.add(sampleList.get(i));
}

System.debug('Check the Order -->'+finalList); // This should be in descending order

 

 

This was selected as the best answer
pallipalli

Hi Sammy ,

 

Thank u very much ...................

Chayan AroraChayan Arora
Hi Everyone,
can u please tell me that how to add list record...??

public class SortingClass 
{
    public List<Employee__c> sortEmp{get;set;}
    public List<Employee__c> sortEmpDESC{get;set;}
    public string sortExp = 'ASC';
    
    Integer totalSize = 0;
    
    public SortingClass()
    {
        sortEmp = new List<Employee__c>();
        
        sortEmp = [select First_Name__c,Last_Name__c,Email__c,Phone_No__c,Designation__c,Salary__c 
                      from Employee__c order by First_Name__c ASC];
    
//        sortExp = 'ASC';
        totalSize = sortEmp.size();
        system.debug('-----totalSize-----'+totalSize);
        
    }
    
    public void sortByName()
    {
        sortEmp.clear();
        
           sortEmpDESC = new List<Employee__c>();
        
        if(sortExp == 'ASC')
        {
            for(Integer i=totalSize-1; i>=0; i--)
            {
                system.debug('-----Index-----'+i);
                
                sortEmpDESC.add(sortEmp.get(i));    //My code is getting error in this line 
                system.debug('----sortEmpDESC----'+sortEmpDESC);
            }
            sortExp = 'DESC';
        }
    
    }
}