Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to override compare method in Comparator

user575089Jul 11 2018 — edited Jul 13 2018

I'm stuck to override compare method in Comparator  for ascending order sorting.

I want to sort in ascending order of  roll number

Code:

class Sortbyroll implements Comparator<Student>

{

    public int compare(Student a, Student b)

    { 

      

  if (a.rollno > b.rollno  ){

   return 1;

  }

    if (a.rollno < b.rollno){

   return -1;

  }

  if (a.rollno == b.rollno){

   return 0;

  }

}

what to do if I want to sort in ascending order ?

This post has been answered by rpc1 on Jul 13 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 10 2018
Added on Jul 11 2018
14 comments
2,376 views