shyam
shyam

Reputation: 1306

sort array list from second item android

i have a array list for using it in spinner ,i have first value i spinner as title and i want to sort array list from second item in the spinner but i dont know how to do this i am using below trick but it sort whole array list including first item which is title so how to statr sorting from second item... my code is below...

 // this is my title ie. "provincia"
    String select2= "Provincia";
       if(!estado1.contains(select2)){
          estado1.add(select2);
          }

     for (int i = 0; i < sitesList1.getEstado().size(); i++)
     {
         if(!estado1.contains(sitesList1.getEstado().get(i))) 
            {

             estado1.add(sitesList1.getEstado().get(i));
                  Collections.sort(estado1);

            }

use below code for show it in spinner...

 final ArrayList<String> estado1 = MainMenu.barrio1;
     final Spinner estado11 = (Spinner) findViewById(R.id.Spinner04);
     ArrayAdapter<String> adapterbarrio = new ArrayAdapter<String>(this, 
           android.R.layout.simple_spinner_item, estado1)

           estado11.setAdapter(adapterbarrio);

Upvotes: 1

Views: 1172

Answers (4)

dten
dten

Reputation: 2374

How about this

    List<String> list = new ArrayList<String>();
    // Fill list
    String title = list.get(0);
    list.remove(0);
    Collections.sort(list);
    list.add(0, title);

Upvotes: 2

Graeme
Graeme

Reputation: 25864

Why not remove the title / only add the title after the list has been sorted?

Upvotes: 2

blessanm86
blessanm86

Reputation: 31779

One way would be to split the arraylist like

estado1.subList(1,estado1.size()-1);

This would return a sublist excluding your title.

Upvotes: 1

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

Use bubble sort! and start at index = 1!

final ArrayList<String> estado1;
for(int i=1; i<estado1.size() ; i++) {
     for(int c=i; c<estado.size() ; c++) {
          if(estado1.get(i).compareTo(estado1.get(c)))
          {
               String temp = estado1.get(i);
               estado1.remove(i);
               estado1.add(i, estado1.get(c));
               estado1.remove(c);
               estado1.add(c, temp);
          }
     }
}

PS: very bad performance

Upvotes: 1

Related Questions