java - How to implement a Comparator to compare names? -


i need implement static method in class student

public static comparator<student> getcompbyname() 

that returns new comparator object student compares 2 students objects attribute 'name'.

iv created think correct, not sure if function supposed requirements given above. new comparators , having hard time them.

public static comparator<student> getcompbyname() {   comparator comp = new comparator<student>(){      @override      public int compare(student s1, student s2)      {         return integer.compare(s1.name.length(), s2.name.length());      }   };  return comp; }   

just reading requirements stated above, not sure supposed comparing 'name', whether length, if equal, etc.. threw in integer compare. clear required , if have done correctly? if not, can let me know need do/change?

looking @ documentation, compare() method should used determine order of 2 students (here, want alphabetically). if wanted check equality, override equals() method of comparator. strings, can use existing methods compare students' name fields.

public static comparator<student> getcompbyname() {      comparator comp = new comparator<student>(){          @override         public int compare(student s1, student s2)         {             s1.name.compareto(s2.name)         }      };      return comp; } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -