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
vijendharvijendhar 

how to display list data in order

Hi all,
 
  I have a scenario  asked in interview, list is loaded with all the countrys, now question is , in the list first need to show UK, US and after that display sorted list of countrys

can any one help how to achive it?

Thanks
 
Best Answer chosen by vijendhar
sachin kadian 5sachin kadian 5
You can do it like this
List<String> countries = new List<String>{'India','Pakistan','UAE','Bermuda'};
countries.sort();
List<String> newCountries = new List<String>{'UK','US'};
        
newCountries.addAll(countries);

System.debug(newCountries);

 

All Answers

sachin kadian 5sachin kadian 5
You can do it like this
List<String> countries = new List<String>{'India','Pakistan','UAE','Bermuda'};
countries.sort();
List<String> newCountries = new List<String>{'UK','US'};
        
newCountries.addAll(countries);

System.debug(newCountries);

 
This was selected as the best answer
vijendharvijendhar
hi sachin,
   these answer is good . but is there any chance by storing all countrys in to one list. List<String> countries = new List<String>{'India','Pakistan','UAE','Bermuda' ,'UK','US' }; IN these how can i display  'UK','US' are first remaining will be in sort order?