4

I have a class that generates random IP addresses. I have to sort this list but I need to use my own logic to compare the two strings.

The way I would prefer to do it is to override the compareTo method in the String class and use Arrays.sort() method but I do not know if this is possible.

I have overridden the compareTo method before but I have always just had it compare instance variables in the same class.

/* Sorts array in ascending order
 *
 * @param    ips     An array of IP Addresses
 * @return           A sorted array of IP Addresses
 */
 public String[] sort(String[] ips)
 {
     String[] arr = ips;

     Arrays.sort(arr);

     return arr;
 }

I know there are other ways to do this but I think that this would be more elegant. Please feel free to let me know if you disagree. I am trying to learn not only how to code, but how to code well.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
jacky
  • 195
  • 6
  • 17
  • Note, it (almost) never makes sense to override `compareTo` to change behaviour. In particular thinjk of `a.compareTo(b)` vs `b.compareTo(a)` where `a` and `b` have different behaviours. – Tom Hawtin - tackline Sep 10 '12 at 17:42

5 Answers5

21

You can't override String's compareTo method because the class is final. But you can provide a custom Comparator to Arrays#sort().

assylias
  • 321,522
  • 82
  • 660
  • 783
8

I realise this doesn't answer your question in the way that you want, but it sounds to me like you need an IP address class rather than a String class. Otherwise your solution will be stringly typed.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
4

It can't be done. String is final; there's no overriding it.

It's your class that should implement its own compareTo and perform the logic.

You can either encapsulate all of it in a custom class or implement a custom Comparator.

Sounds like you're committing the class sin of not thinking about objects enough. String primitives aren't the best encapsulation for custom logic like this. Best to keep it all in one object of your own devising.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    I see. I will definitely keep that in mind but for this assignment we are meant to use Strings... – jacky Sep 10 '12 at 17:03
2

You could try to use the method below in class Arrays:

public static void sort(T[] a, Comparator c);

e.g.

Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                            // provide your comparison logic here
                return 0;
            }
        });
0

you need to implement Comparable interface and implement compareTO(Object) method.

class IP implements Comparable<IP>{
    String ip;
    IP(String ip){
         this.ip=ip
       }

    String getIP(String ip){
    return ip;

    }

public int compareTo(IP){
return ip.compareTo(ip.getIP());
}

}

Now you can invoke Collection.sort(object_ip) and pass object of IP class.

navyad
  • 3,752
  • 7
  • 47
  • 88