I have list which hold records of an object "XYZ"

I have an id of a record of "XYZ" object which should display in list[0] and remaining records next

I am displaying the records using wrapper class.

how would i add the matched record to list of zero

I am adding the records to wrapper class in following way


where v and xyz are objects and g is list of string

          WrapperList.set(0,new VWrappers(v,G,xyz));
        }else {   
       WrapperList.add(new VWrappers(v,G,xyz));

but this would remove and replace it, The initial list[0] is lost


I am using standard set controller for pagination

con = new ApexPages.StandardSetController(Database.query(query));

I am passing the records to a wrapper class and displaying the records on the vf page

I want the replace replace the list[0] so that i can display records of my choice intially

  • 3
    Can you post your code which will further help us understand what you are trying to achieve.
    – codebandit
    Apr 9 '14 at 6:45
  • 2
    I am not very clear what are you asking... can you post your code and refine your questions again Apr 9 '14 at 6:47

You can use a combination of list methods.

//start context
list<string> lst= new list<string>{'aa','bb','cc','dd'};
string var = 'cc';

//find the index of var
integer index; 
for(integer i =0;i< lst.size();i++){
 if(var==lst[i]) index=i;
//may want to throw an exception if index is still null

list<string> tempLst = lst.clone();//backup your collection
lst.clear(); //empty your collection

lst.add(tempLst[index]); //retrieve your desired record and put it on index 0
tempLst.remove(index); //remove your record from the old collection
lst.addAll(tempLst); //add the other data to your list 

system.debug(lst); /// |USER_DEBUG|[22]|DEBUG|(cc, aa, bb, dd)

But, logic like this might imply you should consider exploring the apex map collection type and its methods. Know you can use the map.values() property to still reference it as a list, or can directly iterate over a map in Visualforce too.

  • the index is dynamic
    – user6833
    Apr 9 '14 at 8:36
  • While your comment is rather vague, I agree that the proposed use of .set() was incorrect and have updated my answer. Apr 9 '14 at 8:54
  • In my case its not working i am displaying the records using standard set controller
    – user6833
    Apr 11 '14 at 9:47
  • 1
    This is why you need to add such details to your question, so that it is clear what you truly want an answer to. I'm not sure you can do what you want with a standardSetController. I suggest you update your question, which will attract others who may have an answer. Apr 11 '14 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.