This discussion is archived
4 Replies Latest reply: Jul 2, 2010 9:44 AM by 3004 RSS

Sort list of objects and Comparator.

843793 Newbie
Currently Being Moderated
Hi. I have a problem with list sorting. There is a class with fields like these id (Long), number (String), operator (String), server (String) port (Integer). I have to sort list of these objects in accordance with the conditions specified by the user.

Conditions for sorting:
1. In the first account of the full compliance of the server name and port value (Object Gateway) specifed by the user.
2. When the server name is correct and the port is different from the standard, then the objects should be placed in ascending order of the port.
3. If the server name does not match the model, the order of the objects must reflect the declining value of a parameter port. If the list contains objects of the same port and different name, the order does not matter.

How should Comparator looks like?
  • 1. Re: Sort list of objects and Comparator.
    3004 Newbie
    Currently Being Moderated
    theubk wrote:
    Hi. I have a problem with list sorting. There is a class with fields like these id (Long), number (String), operator (String), server (String) port (Integer). I have to sort list of these objects in accordance with the conditions specified by the user.

    Conditions for sorting:
    1. In the first account of the full compliance of the server name and port value (Object Gateway) specifed by the user.
    2. When the server name is correct and the port is different from the standard, then the objects should be placed in ascending order of the port.
    3. If the server name does not match the model, the order of the objects must reflect the declining value of a parameter port. If the list contains objects of the same port and different name, the order does not matter.

    How should Comparator looks like?
    Given your above rules, if I have any two of your objects how would I determine which one is "greater" or "less"? Turn those rules into Java code and put it into the Comparator's compare() method.
  • 2. Re: Sort list of objects and Comparator.
    843793 Newbie
    Currently Being Moderated
    Ok, this is the solution ;]
    public class GatewayComparator implements Comparator<Gateway> {
    
        private String server;
        private Integer port;
    
        public GatewayComparator(String server, Integer port){
            this.server = server;
            this.port = port;
        }
    
        public int compare(Gateway o1, Gateway o2) {
            int result = 0;
            if (o1.getServer().equals(this.server)) {
                result = -1;
            }
            if (o2.getServer().equals(this.server)) {
                result = 1;
            }
            if (o1.getServer().equals(this.server) && o2.getServer().equals(this.server)) {
                if (o1.getPort().equals(this.port)) {
                    result = -1;
                }
                if (o2.getPort().equals(this.port)) {
                    result = 1;
                } else if (!o1.getPort().equals(this.port) && !o2.getPort().equals(this.port)) {
                    result = o1.getPort().compareTo(o2.getPort());
                }
            } else if (!o1.getServer().equals(this.server) && !o2.getServer().equals(this.server)) {
                if (o1.getPort() > o2.getPort()) {
                    result = -1;
                } else if (o1.getPort() < o2.getPort()) {
                    result = 1;
                } else {
                    result = 0;
                }
            }
            return result;
        }
    }
  • 3. Re: Sort list of objects and Comparator.
    791266 Explorer
    Currently Being Moderated
    O_o
      if (o1.getServer().equals(this.server)) {
                result = -1;
      }
      if (o2.getServer().equals(this.server)) {
                result = 1;
      }
    What do you expect those lines to do?
  • 4. Re: Sort list of objects and Comparator.
    3004 Newbie
    Currently Being Moderated
    Why does your Comparator have state, i.e., member variables? In general, the only state a Comparator would have would be maybe one or more attribute names to use in the comparison. The actual comparison between two objects should depend only on the relative values of those objects' attributes.
    public class PersonComparator implements Comparator<Person> {
      public int compare(Person p1, Person p2) {
        int result;
    
        result = p1.lastName.compareTo(p2.lastName);
        if (result != 0) {
          return result;
        }
    
        result = p1.firstName.compareTo(p2.firstName);
        if (result != 0) {
          return result;
        }
    
    
        return (p1.id < p2.id) ? -1 :
          (p1.id > p2.id) ? 1 :
          0;
      }
    }
    The above compares two Person objects based on last name, and if those are equal, then first name, and if those are equal, then id #. So if a have a list of Persons and I sort it, it will end up like this:
    ID        Last          First
    17        Aardvark      Aaron
    3         Aardvark      Bill
    ...
    2         Smith         John
    5         Smith         Tom
    8         Smith         Tom
    ...