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];
  • 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. Feb 20, 2014 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" Feb 20, 2014 at 21:29
  • I am not creating picklist in code.
    – sfdc
    Feb 20, 2014 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, 2014 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, 2014 at 22:08

3 Answers 3

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

  • I want the sorting like a number.Is it not possible with just order by in soql query
    – sfdc
    Feb 20, 2014 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, 2014 at 22:09
  • 1
    @sfdcfox I have picklist order as 2000,2001,2002,2003 ..but still it is not sorting in picklist order
    – sfdc
    Feb 20, 2014 at 22:12
  • 1
    who wants to make an apexfiddle site
    – Phil Rymek
    Feb 20, 2014 at 22:13
  • @PhilR Like jsfiddle? I'd be game to try that out.
    – sfdcfox
    Feb 20, 2014 at 22:15

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

  • 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
    – sfdc
    Feb 20, 2014 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 Feb 20, 2014 at 21:39

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))


SELECT <whatever> ORDER BY As_Number__c DESC
  • comma is displaying.How to remove comma
    – sfdc
    Feb 24, 2014 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .