You need to sign in to do that
Don't have an account?

sorting a list of non-primitives
In the apex documentation, it says that the sort method for a list can only be used if the list is of primitive types. Does anyone know of a way to work around this with non-primitive types?
I'm building a YouTube integration, and I bind the API results to a custom data type called "video", and I'd like to sort by one of the properties of the video when I have a list of them.
This post might help - bascially, it uses maps to sort lists...
http://community.salesforce.com/t5/Apex-Code-Development/A-gift-for-you-Sorting-a-List-lt-sObject-gt/m-p/107833
Also check this out: http://blog.sforce.com/sforce/2008/09/sorting-collect.html
I use this method all the time.
Hi,
You may have already found an solution to this, but I thought I would post this anyways, as it may benift others.
I was some r&d on the same lines, and stumbled upon the post previous user mentioned. But it was too much for what I wanted to do.
Instead of a list<sObject> to order, I have a list<WrapperClass> to sort, which is not much different when it comes to sorting.
In the wrapper class I had custom Object, boolean, and sort order fields.
What I did was iterate original non-primitieve list I wanted to sort, and copied just the field "sortOrder" into new list<integer>, lets say list<integer>
list<integer> mySortOrder = new list<integer>();
for(list<non-primitive> y : orginalList){
mySortOrder.add(y.sortOrder);
}
//Once I had that, I invloded
mySortOrder.Sort();
Than I have two for loops:
Outter for loop --> iterate over mySortOrder
Inner for loop --> iternate over org list I wanted to sort
for(integer x : mySortOrder){
for(list<non-primitive> y : orginalList){
if(x == y.sortOrder){
// Your business logic //
}
}
}
Depending on your needs, you may want to add some validation. Works great for our needs, and very simple. I have not tested on huge set, but works good for 100-200 records in the list.
Tthought someone can take advantage of this.
regards
Mitesh