0

Im a beginner in java and got into a problem using Collection.sort, i have raed more than 15 question in here and got the main idea but still I got stuck in something. first of all a little bit of background, im using this to sort an arraylist of a specific class, and i want to sort it by the name. i need to create an address book, this is how the class is look like:

public class Contact {
private String name;
private String email;
private String phone;
private String address;

this is what i got while look around in here for the comparator

    public static class ContactComparator implements Comparator<Contact> {
      @Override
      public int compare(Contact first, Contact second) {
         int f = first.getName().compareTo(second.getName());
         return f;
         }
      }

and this is the command for the sort

Collections.sort(contacts , new ContactComparator());

now this is the problem it gives me:

enter image description here

what is wrong with it? i just can't seems to find it

thank you all very much helpers!

shimon
  • 86
  • 7

2 Answers2

2

you should call comparator method from some other method and i think you are not doing that

like following

method(){
//call for comparison
}

you are directly trying to call it from class.

dev2d
  • 4,245
  • 3
  • 31
  • 54
  • thank you very much, you were almost there, insted of using my method in a different place, i moved the command for the sorting to the methods that add my people to the list – shimon Apr 15 '14 at 08:45
0

Your call for compare should not be in the contactComprator class, it should be in some method and not "out in the open", java don't knows how to handle it, python can(for example) but not java.

Gigalala
  • 439
  • 1
  • 8
  • 24