0

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);
}
user1165694
  • 1,255
  • 2
  • 18
  • 29
  • Possible repost? http://stackoverflow.com/questions/5815423/sorting-arraylist-in-android-in-alphabetical-order-case-insensitive ... only difference is you get your array list as the contents of your listview, if you don't already have the contents as an array list. – gkiar Jul 17 '12 at 13:26

2 Answers2

7

From your arraylist ArrayList<LoginDetails> loginArrayList;

i preffered you to use Comparator.

  Collections.sort(loginArrayList,new Comparator<LoginDetails>() {

    @Override
    public int compare(LoginDetails lhs, LoginDetails rhs) {

        return lhs.name.compareTo(rhs.name);

    }
});
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

Use Collections.sort(). From the doc:

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

Collections.sort(loginArrayList )
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • implements the above suggestions but the results are the same. Listview is not sorted in alphabetical order. I’m sure this is simple but my minimal experience with this it not allowing me to implement properly. Any assistance or direction will be appreciated. – user1165694 Jul 22 '12 at 23:33