1

I am Java beginer and I have to make a program that enters a String using command line and then prints the number of the entered words, the entered words and the sorted words. I can do everything except the sorting. I know I have to use compareTo, but I don't know how to make the method work. Would love to get some help!

Here's my code so far:

class Sort{
    public static void main(String args[]){
         int count=args.length;
         System.out.println("\nYou've enetered "+count+" word and they are:");
         for(int i=0;i<count;i++)
         {System.out.print(args[i]+" ");}
         System.out.println("\nThe sorted words are:");
    }
}
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Daniela Gocheva
  • 63
  • 2
  • 11

2 Answers2

1

Since you are required to use compareTo, you can implement Collections.sort.

After all values are added in your array, just provide this array to Collections.sort() along with a custom Comparator. But the problem is that the Collections.sort() wouldn't accept an String array, so you also have to convert it to a list using Arrays.asList(yourArray) method.

Suppose this is your array,

String [] args = new String[]{"dddd","cccc","bbbb", "aaaa"};

Now let's use Collections.sort after converting your array to a list and provide it with a Comparator.

Collections.sort(Arrays.asList(args),new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
});

simple, isn't it ?

If you want to print the sorted values now,

for (String p : args ){
    System.out.println(p);
}

Outputs

aaaa
bbbbb
cccc
dddd

Just for your information, if you want to sort in reverse order, replace return o1.compareTo(o2) with return o2.compareTo(o1)

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
0

You can use Arrays.sort for sorting and Arrays.toString to convert to String the given array:

class Sort{
    public static void main(String args[]){
        System.out.printf("\nYou've enetered %d words and they are:", args.length);
        System.out.println(Arrays.toString(args));

        Arrays.sort(args);
        System.out.println("\nThe sorted words are:");
        System.out.println(Arrays.toString(args));
    }
 }
manzur
  • 682
  • 3
  • 7
  • 2
    OP clearly stated he needs to use `compareTo()`, and doing what likely is his homework for him isn't going to benefit him in any way. – Drew Kennedy Feb 03 '15 at 21:12
  • @Drew `sort` uses `comapreTo(...)`. Sure, you may be right, but let the OP mention this him/herself. – Bart Kiers Feb 03 '15 at 21:14
  • @BartKiers it's stated in the question: *I can do everything except the sorting. **I know I have to use compareTo**, but I don't know how to make the method work.* – Luiggi Mendoza Feb 03 '15 at 21:14
  • How do you suppose to implement `compareTo` for the `String` class? – manzur Feb 03 '15 at 21:17
  • @LuiggiMendoza, like I said, that may well be the case, but the OP didn't explicitly mention s/he can't use the built-in sort. The OP might not be aware of `Arrays.sort(...)` and it's *that* fact s/he means. Again, it's speculation. What's wrong with letting the OP clarify? – Bart Kiers Feb 03 '15 at 21:17
  • If I had 2 Strings I was going to do it myself, but the command line entering is confusing me... – Daniela Gocheva Feb 03 '15 at 21:19
  • @manzur the question is not about implementing `compareTo` for `String` class. It's about sorting a `String[]` using `String#compareTo`. This looks like homework, so seems like OP has to implement a sorting algorithm like bubble sort or quicksort, depending on his/her knowledge on the topic. – Luiggi Mendoza Feb 03 '15 at 21:22
  • @BartKiers OP has clarified this in comments on the question but I cannot edit the question since I approved the edit before reading the comments. – Luiggi Mendoza Feb 03 '15 at 21:27