Search code examples
javaandroidlistviewalphabetical

List View in alphabetical order


I have created a android list view in Eclipse that I would like to to sort the data entered in alphabetical order by the first character. Not sure how to accomplish this.

private ListView loginL;
private Button bLogin;

private ListAdapter loginListAdapter;

private ArrayList<LoginDetails> loginArrayList;

@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_listview);


    loginL = (ListV)

findViewById(R.id.loginlist); loginL.setOnItemClickListener(this);

    bLogin = (Button)

findViewById(R.id.button3); webLogin.setOnClickListener(this);

    loginArrayList = new ArrayList<LoginDetails>();
    loginLAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
    loginL.setAdapter(loginLAdapter);
}

Solution

  • Use Collections.sort(). From the doc:

    Sorts the specified list into ascending order, according to the natural ordering of its elements

    Collections.sort(loginArrayList )