'Salesforce apex, add values to a list and then display them in a specific order
I've a multi-select pick list that has initials in a specific order (not alphabetical), those initials are for team ranking purposes. so every time i add a member to the team, he must be saved and displayed on the report according to his ranking (initials).
for instance, if my rankings are: AB, XY, FC, and DQ, and then i created two members (FC, AB), the one with AB MUST be displayed on the report before FC.
I've created a list that has the initials in the desired order, and every time a member is created it's being added to a different list. what I'm getting now is, the last member created (last member added to the list) is displayed at the end of the report, even if his ranking is higher than those were added before him. so my question is: How do i save the members that I add to the list according to my rankings order?
Thank you guys for your efforts!
Solution 1:[1]
As far as I know, you can only add items to a list by using the add or addAll functions which puts the new item at the end of the list. You can replace items using the numerical index as well but that would not provide the insert functionality you seem to be referring to. However, you can add your new list members and then call the sort function on the list to sort things in ascending order. This will give the result you're after. Here's an example:
List<String> rankings = new List<String>();
rankings.add('XY');
rankings.add('AB');
rankings.add('FC'); // you should now have ['XY', 'AB', 'FC']
rankings.sort(); // should rearrange the list items to be ['AB', 'FC', 'XY']
More info: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_List_sort.htm
However, this only leaves you with a sorted list of strings and no link to associated records. If you're adding records in a controller or via ajax then you could also just run a soql query for the members and order by your ranking field to get a new list with your ordering applied. Without seeing your implementation or knowing any details about what you're doing it's hard to say what the best approach would be but as I've shown above it is possible to keep a list object sorted even after adding to it.
Solution 2:[2]
Why do you have a multi select picklist for rankings? Each member would only have one ranking, right? If this is the case, a standard picklist should do the trick.
You should create a SOQL query for the group members you are trying to create a list of and then use the Order By clause to order by the picklist field values.
I.E.
Assumptions:
Object - User
Ranking Picklist field - ranking__c //Custom field on user object containing the ranking,
Team - team__c //custom field on user object containing the team number of the member.
List users = new List();
users = [SELECT ID from user where ranking_c != NULL AND Team_c = 'Some Value' ORDER BY ranking__c];
//Some other code doing something with the sorted list of users. Note the order by clause is sorting by the values in the custom "ranking__c" field.
I hope you find this useful.
Regards, J
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vscuorzo |
| Solution 2 | Berg |
