Sorting in Descending order using Comparator
<p>I'm trying to sort the list in descending order using Comparator Interface. But the values are not sorted in descending order. Not sure what i'm doing wrong here.</p>
<pre><code>public class Student {

    int rollNo;
    String name;
    int age;
    
    public Student(int RollNo, String Name, int Age){
        this.rollNo = RollNo;
        this.name = Name;
        this.age = Age;
    }
}

public class AgeComparator implements Comparator&lt;Student&gt;{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age &gt; o2.age ? 1 :(o1.age &lt; o2.age ? -1 : 0); //Ascending
         
        //return o1.age &lt; o2.age ? -1 :(o1.age &gt; o2.age ? 1 : 0); // Descending
    }

}

public class Comparator_Sort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        ArrayList&lt;Student&gt; al = new ArrayList&lt;Student&gt;();
        al.add(new Student(5978, &quot;Vishnu&quot;, 50));
        al.add(new Student(5979, &quot;Vasanth&quot;, 30));
        al.add(new Student(5980, &quot;Santhosh&quot;, 40));
        al.add(new Student(5981, &quot;Santhosh&quot;, 20));
        al.add(new Student(5982, &quot;Santhosh&quot;, 10));
        al.add(new Student(5983, &quot;Santhosh&quot;, 5));
        
        
        Collections.sort(al, new AgeComparator());
        
        for(Student s : al){
            System.out.println(s.rollNo+&quot; &quot;+s.name+&quot; &quot;+s.age);
        }
    }
}
</code></pre>
<p>I can be able to sort the list in ascending order, whereas i'm unable to do it for descending order<br></p>
<pre><code>return o1.age &gt; o2.age ? 1 :(o1.age &lt; o2.age ? -1 : 0); //Sorted in Ascending
return o1.age &lt; o2.age ? -1 :(o1.age &gt; o2.age ? 1 : 0); // Not sorted in Descending
</code></pre>
<p>Comparator documentation -- Returns: a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. The source is found from <a href="https://stackoverflow.com/questions/6478515/return-type-from-a-comparator">here</a><br><br></p>
<p>Can anyone tell me why the sorting for descending is not working?</p>