Pages

Friday, February 1, 2013

Sorting ArrayList (String ArrayList and custom class)

 1. String array List order by... use to Collection.sort(arrayList)..


List<String> arrString = new ArrayList<String>();
  
Collections.sort(arrString, new Comparator<String>(){
      public int compare(String obj1, String obj2)
      {
            // TODO Auto-generated method stub
            return obj1.compareToIgnoreCase(obj2);
      }
});


2. Custom class Array List order by.. use to Collection.sort(arraylist)..


//student.java class define public String object.....

class student 
{
      public String name;
      public int rollno;
      public String phone_no;
}

//ArrayList....


List<student> arrStudent = new ArrayList<student>();
arrStudent.add( new student("dddd",45,"755555"));
arrStudent.add( new student("zzzz",45,"345345"));
arrStudent.add( new student("cccc",45,"888888"));
arrStudent.add( new student("qqqq",1,"123123"));
arrStudent.add( new student("llll",12,"123123"));
arrStudent.add( new student("pppp",65,"123123"));
arrStudent.add( new student("hhhh",4,"123123"));
arrStudent.add( new student("bbbb",78,"123123"));
arrStudent.add( new student("abbb",3,"123123"));

//then i want to order by student name then... use this ..

Collections.sort(arrStudent, new Comparator<student>(){
      public int compare(student obj1, student obj2)
      {
            // TODO Auto-generated method stub
            return obj1.name.compareToIgnoreCase(obj2.name);
      }
});

//Sorting student roll number Ascending Order

 Collections.sort(arrStudent, new Comparator<student>(){
  public int compare(student obj1, student obj2)
  {
    // TODO Auto-generated method stub
    return (obj1.rollno < obj2.rollno) ? -1: (obj1.rollno > obj2.rollno) ? 1:0 ;
  }
}); 


//Sorting student roll number Descending Order

 Collections.sort(arrStudent, new Comparator<student>(){
  public int compare(student obj1, student obj2)
  {
    // TODO Auto-generated method stub
    return (obj1.rollno > obj2.rollno) ? -1: (obj1.rollno > obj2.rollno) ? 1:0 ;
  }
}); 


Other Post

- How to use custom contains method in an ArrayList in android?


10 comments:

  1. if i want to sort another list in this class what can i do

    ReplyDelete
  2. thanks It is to use
    It Solve my Problem :)

    ReplyDelete
  3. Thank You... Very nice Example...

    ReplyDelete
  4. this code Only change in descending order in which > to <

    ReplyDelete
  5. when i click on button but its not working.. so help me

    ReplyDelete
  6. como filtrar el arreglo usando un edittext, por ejemplo:

    List arrStudent = new ArrayList();
    arrStudent.add( new student("dddd",45,"755555"));
    arrStudent.add( new student("zzzz",45,"345345"));
    arrStudent.add( new student("cccc",45,"888888"));
    arrStudent.add( new student("qqqq",1,"123123"));
    arrStudent.add( new student("llll",12,"123123"));
    arrStudent.add( new student("pppp",65,"123123"));
    arrStudent.add( new student("hhhh",4,"123123"));
    arrStudent.add( new student("bbbb",78,"1111111"));
    arrStudent.add( new student("abbb",3,"2222222"));



    y en el edittext ingreso "88888"

    ReplyDelete