0

I am creating a method compareTo(AltString altStr2) that sorts strings by their length (shortest to longest).

However, I would like to go the extra mile and check for strings that are of the same length. In that case, I figure it is best to sort the strings lexicographically, so that they are sorted in the same way they would appear in the dictionary. So far my code is below.

public class AltString {

    String internalStr;

      public AltString(String str) {
        internalStr = str;
      }

      public String toString() {
        return internalStr;
      }

public int compareTo(AltString altStr2) {
      if (this.internalStr.length() < altStr2.internalStr.length()) {
          return -1;
      } else if (this.internalStr.length() > altStr2.internalStr.length()) {
          return 1;
      } else {
          int idx = 0;
          while (this.internalStr.charAt(idx) != altStr2.internalStr.charAt(idx)) {
              if (this.internalStr.charAt(idx) < altStr2.internalStr.charAt(idx)) {
                  return -1;
              }else if (this.internalStr.charAt(idx) > altStr2.internalStr.charAt(idx)) {
                  return 1;
              } else {
                  idx += 1;

public static void main(String[] args) {
        // some test code for the AltString class
        String [] list = {"fortran", "java", "perl", "python", "php", "javascrip", "c", "c++", "c#", "ruby"};
        AltString [] alist = new AltString[list.length];
        for (int i=0; i<alist.length; i++) {
          alist[i] = new AltString(list[i]);
        }

        Arrays.sort(list);
        Arrays.sort(alist);
        System.out.println("String sort:");
        for (int i=0; i<list.length; i++) {
          System.out.println(list[i]);
        }

        System.out.println("\nAltString sort:");
        for (int i=0; i<alist.length; i++) {
          System.out.println(alist[i]);
        }
      }

The part I am must stuck on is comparing the strings lexicographically. Currently, I have my code setup so that I enter a while-loop and compare each char.

My question is, is this the most efficient way to do this, or is there a better way to compare each string lexicographically in the cases where the strings are the same length?

Omar N
  • 1,720
  • 2
  • 21
  • 33
  • 6
    You do know lexicographic sorting is the default sorting of a String? – Tunaki Nov 15 '15 at 20:00
  • Sorry, what do you mean? – Omar N Nov 15 '15 at 20:04
  • 3
    @OmarN Tunaki means that in the `else` bit you can just do `return this.internalStr.compareTo(altStr2.internalStr);`. – Paul Boddington Nov 15 '15 at 20:05
  • @PaulBoddington, I thought about doing that, but wouldn't that be recursion at that point? – Omar N Nov 15 '15 at 20:07
  • @OmarN No, unless `internalStr` is an `AltString`. We are all assuming it's a `String`, but you don't actually say that... – Paul Boddington Nov 15 '15 at 20:08
  • [See the compareTo method](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)) – Caffeinated Nov 15 '15 at 20:08
  • @OmarN - What is an AltString ? can we see rest of code? – Caffeinated Nov 15 '15 at 20:10
  • 1
    @PaulBoddington, understood. I added the rest of the code. – Omar N Nov 15 '15 at 20:10
  • In addition to using String.compareTo(), you should also compare the lengths with Integer.compare(). – JB Nizet Nov 15 '15 at 20:11
  • `while ==` . Also **[java.text.Normalizer](http://docs.oracle.com/javase/7/docs/api/java/text/Normalizer.html)** could be useful as one Unicode text could be written as different Strings. `ĉ` as one code point or two: `c` plus a zero-length `^`. The second form is nicer for sorting, – Joop Eggen Nov 15 '15 at 20:17
  • Thank you for the help everyone. I guess the main question here is, does adding `return this.internalStr.compareTo(altStr2.internalStr);` after the `else` count as recursion in this case? I thought that it did, and so that is why I used the while-loop instead. – Omar N Nov 15 '15 at 20:21
  • 1
    @OmarN No it is not recursion. Recursion is when you call a method from inside itself. You are not doing that. `String.compareTo` is a different method from `AltString.compareTo`. They just happen to have the same name. – Paul Boddington Nov 15 '15 at 20:28
  • @PaulBoddington, thanks Paul. If you add that as the answer, I will go ahead and accept it. Thanks again. – Omar N Nov 15 '15 at 20:50
  • @OmarN Done. I've made it a community wiki as it's really Tunaki and JB Nizet's answer, not mine. – Paul Boddington Nov 15 '15 at 20:55

1 Answers1

3

Following the suggestions of Tunaki and JB Nizet, you can use Integer.compare and String.compareTo. Using String.compareTo does not count as recursion. Recursion is when you call a method from itself, but String.compareTo is a different method from AltString.compareTo.

public int compareTo(AltString altStr2) {
    int temp = Integer.compare(this.internalStr.length(), altStr2.internalStr.length());
    return temp != 0 ? temp : this.internalStr.compareTo(altStr2.internalStr);  
}
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116