Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a picklist with values 2000,2001,2002,2003,...When I used below query records are not sorting on visualforce page . From order by docs sorting of picklist is a special case "picklist sorting is defined by the picklist sort determined during setup."

 list<Some_Object__c> olist = [SELECT id,Your_Picklist_field__c FROM Some_Object__c ORDER BY Your_Picklist_field__c DESC];
share|improve this question
    
can you add the code where you create the picklist? If you use are using a map whatsoever, that's probably what's causing the mis-ordering. –  amatorVitae Feb 20 at 21:29
    
Also, your soql should read: "SELECT id,Your_Picklist_field__c FROM Some_Object__c ORDER BY Your_Picklist_field__c DESC" –  amatorVitae Feb 20 at 21:29
    
I am not creating picklist in code. –  sf.dev Feb 20 at 21:31
    
Perhaps you are wanting it to sort like a list of numbers, but instead it is sorting like a list of strings. –  Phil Rymek Feb 20 at 21:54
2  
@PhilR The same effect is visible in reports. Try it out sometime; sort an opportunity report by stage name. –  sfdcfox Feb 20 at 22:08
show 2 more comments

3 Answers 3

Go to the Object setup, go the Picklist field in question, and there are options there where you can change the ordering of the Picklist values

share|improve this answer
    
picklist values are sorted under setup.But when you try to display muliple records on vf page order by this picklist ...sorting is not working –  sf.dev Feb 20 at 21:35
    
you mean you need different picklist-sorts for different Record Types? If what SF is naturally giving is not sufficing, I suggest generating your own picklists in your VF Controller –  amatorVitae Feb 20 at 21:39
add comment
picklist sorting is defined by the picklist sort determined during setup.

this means your data is returned in same order as in picklist field.

You can use Comparable Interface to sort your list by any wanted value

global class Employee implements Comparable {

public Long id;
public String name;
public String phone;

// Constructor 

public Employee(Long i, String n, String p) {
    id = i;
    name = n;
    phone = p;
}

// Implement the compareTo() method 

global Integer compareTo(Object compareTo) {
    Employee compareToEmp = (Employee)compareTo;
    if (id == compareToEmp.id) return 0;
    if (id > compareToEmp.id) return 1;
    return -1;        
}
}

check http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_comparable.htm

share|improve this answer
    
I want the sorting like a number.Is it not possible with just order by in soql query –  sf.dev Feb 20 at 22:07
1  
@sf.dev SOQL ordering is indeed in picklist order, not numeric order. If you wanted numeric order, use a number-type field. –  sfdcfox Feb 20 at 22:09
    
@sfdcfox I have picklist order as 2000,2001,2002,2003 ..but still it is not sorting in picklist order –  sf.dev Feb 20 at 22:12
1  
who wants to make an apexfiddle site –  Phil Rymek Feb 20 at 22:13
    
@PhilR Like jsfiddle? I'd be game to try that out. –  sfdcfox Feb 20 at 22:15
add comment

Another method would be using a formula field to convert the picklist to a type number, then sort on that formula field.

Formula Field

As_Number__c = VALUE(TEXT(My_Picklist__c))

SOQL

SELECT <whatever> ORDER BY As_Number__c DESC
share|improve this answer
    
comma is displaying.How to remove comma –  sf.dev Feb 24 at 23:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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