-2

Possible Duplicate:
How to sort an arraylist of objects by a property?

    public static void main(String args[]){

            List<Emp> unsortList = new ArrayList<Emp>();

            unsortList.add(new Emp(109));
            unsortList.add(new Emp(106));
            unsortList.add(new Emp(103));
            unsortList.add(new Emp(108));
            unsortList.add(new Emp(101));
    }

public class Emp {
Integer eid;
public Emp(Integer eid) {
    this.eid=eid;
}
}

Emp is user defined class & stored in ArrayList. How sort ArrayList.

Community
  • 1
  • 1
Vhasure Prashant
  • 188
  • 5
  • 15
  • 3
    You probably should have tried Google before asking your question here, there are LOTS of info on this topic. – Egor Oct 28 '12 at 09:53

2 Answers2

2

Check out Collections#sort(List list, Comparator c)

It allows you supply your own Comparator which you can use to define how the objects will be matched

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Implement Comparable<Emp> and use Collections.sort to sort the list

public class Emp implements Comparable<Emp> {
    Integer eid;

    public Emp(Integer eid) {
        this.eid = eid;
    }

    @Override
    public int compareTo(Emp o) {
        return eid.compareTo(o.eid);
    }
}
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72