Thursday, March 12, 2015

Sorting a ArrayList in android

If you are having a string arraylist which contains names of people and you want to sort the arraylist in alphabetical order than use the below code. 

Sorting a ArrayList 

Most easy method to do so is..using Collections .You can use Collections to perform sorting or any other function on ArrayList.


for example :

To sort an arraylist according to name:


Collections.sort(YourArrayList, new Comparator<String>() {     @Override     public int compare(String  text1, String text2) {      return text1.compareToIgnoreCase(        text2);     }    });


Reverse a arraylist :


         Collections.reverse(YourArrayList);

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design