1

I have spinner with array list thats work fine, but i want to sort out the datas from a to z (example: apple,ball,cat,dog...)order. I submit my code below

    ArrayList<String> SourceArray = new ArrayList<String>();
    Spinner Sourcespinner;// = new Spinner(this);
    Sourcespinner = (Spinner)findViewById(R.id.Spinner1);
    ArrayAdapter<String> SourceArrayAdapter = new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item,SourceArray);
    SourceArrayAdapter.add("Chennai");
    SourceArrayAdapter.add("Mumbai");
    SourceArrayAdapter.add("Kolkatta");
    SourceArrayAdapter.add("Delhi");
    Sourcespinner.setAdapter(SourceArrayAdapter);`

I don't know how to do sorting for this

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
sivan esan
  • 143
  • 1
  • 3
  • 8

2 Answers2

1

you can use this to sort your data

Collections.sort(SourceArray);
Mert
  • 6,432
  • 6
  • 32
  • 68
  • yes , i got it ,, is it possible to show the list of particular alphabet if i pressed particular alphabet (example: if i press f is it possible to display the list starting with f that means fat,fit,fog like that) – sivan esan Jul 13 '12 at 10:28
  • I am not sure to do that easly. – Mert Jul 13 '12 at 10:38
0

Try to add data to the ArrayList and just use the Collections class to sort for you:

Collections.sort(SourceArray);

If you need to add your own objects they need to implement the Comparable interface and implement the method compareTo(). When changing the ArrayList's data make sure to notify the adapter that new data might have been added by using this code:

SourceArrayAdapter.notifyDataSetChanged();
Tim
  • 6,692
  • 2
  • 25
  • 30