Ranjitsingh Chandel
Ranjitsingh Chandel

Reputation: 1489

How to make my list in ascending order in Android

I want to make my list in ascending order how can i do pragmatically. I tryed but not success.

CODE:

PhonebookAdapter adapter; // My Phonebook Adapter Class
final List<Phonebook> listOfPhonebook = new ArrayList<Phonebook>();
...
...
adapter = new PhonebookAdapter(this, listOfPhonebook);
list.setAdapter(adapter);

Upvotes: 0

Views: 2486

Answers (2)

harshit
harshit

Reputation: 3846

Try using Collections.sort(list); You may need to import java.util.Collections;

For using custom properties for sorting you will have to implement Comparator Example:

public class CustomComparator implements Comparator<Phonebook> {
   @Override
   public int compare(Phonebook p1, Phonebook p2) {
      return p1.name.compareTo(p2.name);
   }
}

And for sorting you will have to:

Collections.sort(list, new CustomComparator());

Upvotes: 2

SBJ
SBJ

Reputation: 4139

        Collections.sort(listOfPhonebook , new Comparator<Phonebook>() {
            public int compare(Phonebook phn1, Phonebook phn2) {
                return phn1.name.compareTo(phn2.name);
            }
        });

try this , assume name is variable of phonebook class and sorting as per name.

Upvotes: 0

Related Questions