SusonS3
SusonS3

Reputation: 31

Sorting on Java using compareTo without using an arraylist

I am a beginner to Java and I have been trying to use the compareTo method in order to sort three cities. My three test words are Legion, LSD and Chunkey. To identify the errors, I have each output possibilities with a number from 1-6. For output 1 it's (Chunkey, LSD, Legion), 2 = (legion, LSD, Chunkey, 4 = (Chunkey, Legion, LSD). The output tends to make no sense when I believe I'm following the right logic that when two strings are compared and the value is a negative value then the first value comes first and then the second value. Yet even doing that, it's not ordered correct. Please help! Thank you!

import java.util.Scanner;
public class OrderingCity {
public static void main (String []args){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the First City");
    String c1 = input.nextLine();
    System.out.println("Enter the Second City");
    String c2 = input.nextLine();
    System.out.println("Enter the Third City");
    String c3 = input.nextLine();

    //When the value is less than 0, the first value is first while the second value is second.

    //When the first city is compared to the second and if it returns a negative value (<0) then
    //the first city comes first and when the value of the second city is compared to the third
    //city and it returns a negative value then the second city comes before the third city
    if (c1.compareTo(c2) < 0 && c2.compareTo(c3) < 0){ 
        System.out.println(c1 + " 1 " + c2 + " " + c3);
    }

    //When the first city is compared to the second and it returns a positive value (>0) then
    //the second city comes first and when the value of the first city is compared to the third
    //city and it returns a positive value (>0) then third city comes before the first.
    else if (c1.compareTo(c2) > 0 && c1.compareTo(c3) > 0){
        System.out.println(c2 + " 2 " + c3 + " " + c1);
    }


    else if (c2.compareTo(c3) < 0 && c3.compareTo(c1) < 0){
        System.out.println(c2 + " 3 " + c3 + " " + c1);
    }


    else if (c2.compareTo(c3) > 0 && c2.compareTo(c1) > 0){
        System.out.println(c3 + " 4 " + c1 + " " + c2 );
    }

    else if (c3.compareTo(c1) < 0 && c1.compareTo(c2) < 0){
        System.out.println(c3 + " 5 " + c1 + " " + c2);
    }

    else if (c3.compareTo(c1) > 0 && c3.compareTo(c2) > 0) { 
        System.out.println(c1 + " 6 " + c2 + " " + c3);
    }


}
}

Upvotes: 3

Views: 578

Answers (4)

Andreas
Andreas

Reputation: 159114

To simply sort 3 values manually, use the mini bubble sort logic in the answer by @hlucasfranca.

You did say "without using an arraylist", but if that doesn't preclude using an array, this might be simpler:

String[] arr = { c1, c2, c3 };
Arrays.sort(arr);
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);

However, when you sort using natural order (String.compareTo()), it sorts by Unicode (UTF-16) code point, which means that LSD will sort before Legion.

You could simply use String.compareToIgnoreCase(), and you would get the desired ordering. For the array version, use String.CASE_INSENSITIVE_ORDER:

String[] arr = { c1, c2, c3 };
Arrays.sort(arr, String.CASE_INSENSITIVE_ORDER);
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);

That will give the right output:

Chunkey Legion LSD

However that still only works for plain English. To sort alphabetically in any particular language, like the index in a book, you need a Collator. You can use the Collator.compare() method in the bubble sort logic, or give it to the Arrays.sort() method:

String[] arr = { c1, c2, c3 };
Arrays.sort(arr, Collator.getInstance());
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);

Example: For the french words étouffer, deuxième, and Hôtel (random french words with accents), the sorting would be:

Hôtel deuxième étouffer    // Natural order
deuxième Hôtel étouffer    // Case-insensitive order
deuxième étouffer Hôtel    // Collator

Upvotes: 0

hlucasfranca
hlucasfranca

Reputation: 270

This should help:

import java.util.Scanner;

public class OrderingCity {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the First City");
        String c1 = input.nextLine();

        System.out.println("Enter the Second City");
        String c2 = input.nextLine();

        System.out.println("Enter the Third City");
        String c3 = input.nextLine();

        String temp;

        // Example:  c b a

        // c > b
        if (c1.compareTo(c2) > 0) {
            temp = c1;
            c1 = c2;
            c2 = temp;
        }
        // b c a

        // c > a
        if (c2.compareTo(c3) > 0) {
            temp = c2;
            c2 = c3;
            c3 = temp;
        }
        // b a c

        // a > b
        if (c1.compareTo(c2) > 0) {
            temp = c1;
            c1 = c2;
            c2 = temp;
        }
        // a b c

        System.out.printf("%s %s %s", c1, c2, c3);
    }
}

Upvotes: 2

jr593
jr593

Reputation: 277

When you execute

c1.compareTo(c2) > 0 && c1.compareTo(c3) > 0

you are not checking c2 relationship to c3

which means that (for example) 'MMM' , 'JJJ' , 'CCC' only compares MMM to JJJ , and MMM to CCC , but not JJJ to CCC so the ordering of c2 and c3 is ignored

Upvotes: 1

vishal
vishal

Reputation: 72

Add c1,c2 and c3 to array list and call Collections.sort on it as explained at below link :

How can I sort a List alphabetically?

Upvotes: -2

Related Questions