java...based on Collections
<pre><code>import java.util.ArrayList;
import java.util.*;

class Employee1 implements Comparable&lt;Employee1&gt; {
    int empno;

    String name;
    Employee1(int i,String j) {
        empno=i;
        name=j;
    }

    public int compareTo(Employee1 e1) {
        Employee1 e2=this;
        Integer i1=e1.empno;
        Integer i2=e2.empno;
        return i1.compareTo(i2);
    }
}

public class Employee {
    public static void display(ArrayList&lt;Employee1&gt; i) {
        for(Employee1 o:i){
            System.out.println(o.empno);
        }
    }

    public static void main(String[] args) {
        ArrayList&lt;Employee1&gt; ob=new ArrayList&lt;Employee1&gt;();
        Employee1 e1=new Employee1(1001,"A");
        Employee1 e2=new Employee1(1002,"B");
        ob.add(e2);
        ob.add(e1);
        Collections.sort(ob);
        display(ob);
    }
}
</code></pre>

<p>The output should be <code>1001 1002</code> but instead the output is <code>1002 1001</code>.</p>

<p>First I passed the <code>e2</code> object with <code>empno</code> to the List and then 
<code>e1</code> with <code>empno</code> of 1001</p>

<p>Due to the <code>compareTo</code> method in <code>Employee1</code> - <code>e1.compareTo(e2);</code> should be the same as <code>1001.compareTo(1002);</code> So why are they showing in the wrong order?</p>
