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
singhd62singhd62 

Custom Sorting on Multiple fields

Hi,

Want to perform custom sorting on multiple fields in apex same as Excel which narrows down the list lets say from 100 to 5 using sort type (asc or desc)

Thanks,
singhd62
Ramu_SFDCRamu_SFDC
I guess you are asking about some feature like sorting and filtering on multiple rows in excel which narrows down the results to lesser number. To do this you can use the standard list views as explained in the link http://www.shellblack.com/administration/how-to-filter-records-from-a-tab-using-views/

If my understanding is incorrect, please provide more details.
singhd62singhd62
I have a list of records which I will be passing to method which does the sorting on 4 fields and gives me the sorted list in return.

eg:
acc1 10 Status1 900
acc2 20 Status2 907
acc3 80 Status3 902
acc4 10 Status1 800
acc5 20 Status5 904
acc6 40 Status6 905
acc7 40 Status7 906
acc8 30 Status8 901
acc9 25 Status9 908
acc10 33 Status10 909
  
below will be the sorted list:
acc4 10 Status1 800
acc1 10 Status1 900
acc2 20 Status2 907
acc5 20 Status5 904
acc9 25 Status9 908
acc8 30 Status8 901
acc10 33 Status10 909
acc6 40 Status6 905
acc7 40 Status7 906
acc3 80 Status3 902
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)

Hi,

You can try out datatable multi ordering on columns plugin in your vf page.

For more infor please go through http://www.datatables.net/examples/basic_init/multi_col_sort.html (http://www.datatables.net/examples/basic_init/multi_col_sort.html)

Thanks,
N.J

singhd62singhd62
It is not requried to show on the datatable, sorting is needed while processing bulk records
Ramu_SFDCRamu_SFDC
The below article has some sample queries to sort multiple columns but not sure if it works for 4 columns. The examples outlined were done for two columns something as this SELECT Name FROM Contact ORDER BY LastName DESC, FirstName DESC

Review the article at the below link for more examples

http://developer.force.com/cookbook/recipe/sorting-query-results
singhd62singhd62
The query will be done outside the for loop.

And then we iterate over it by applying sorting in the for loop as there is a field which gets updated during the execution. And again at the start of the loop we need to sort on that runtime value.
David "w00t!" LiuDavid "w00t!" Liu
You can create a formula field that combines all four fields into one (just as you have typed in your sample), then sort on that field instead in the background
singhd62singhd62
Sorting on formula field will either be asc or desc. But whereas in my case, few fields will be asc and rest will be in desc
David "w00t!" LiuDavid "w00t!" Liu
In that case you might want to use this interface, where you can define a custom sorting algorithm:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_comparable.htm
singhd62singhd62
Can we do sorting on AND basis using Comparable ?

I mean to say the first sorted list need to be retained while applying 2nd field sorting.. 


David "w00t!" LiuDavid "w00t!" Liu
Yup you sure can!

You'll be able to do it like this (a and b are the comparable values):

if (a > b) {
    compare = 1;
} else if (a < b) {
    compare = -1;
} else {
    if (c > d) {
        compare = -1;
    } else if (c < d) {
        compare = 1;
    }
}