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 want to make the set to display as Unorderlist. So that it will display the values in an order that I have initialized.

Is there any way to achieve it?

Set<String> values = new Set<String>{'SZ','SA','EL','SP','DP'};
share|improve this question
add comment

2 Answers

Sets are unordered lists that don't contain duplicates. If you want to maintain the order, use a List.

share|improve this answer
add comment

Sets are un-ordered by their very nature. If you need an ordered list you should use a List<String> collection. If you need to ensure things are unique and other set features, then you can just get a list for rendering only by adding all of the set elements to a list:

public List<String> getOrderedList()
{
  List<String> orderedValues = new List<String>();
  orderedValues.addAll(values);
  orderedValues.sort();
  return orderedValues;
}

In your scenario you probably don't want the elements sorted alphabetically, so you'd be better off initialising the list first, and then adding all the list's elements to a set if that functionality is required.

share|improve this answer
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.