4

I am new to java.

I'm trying to sort an ArrayList of String arrays using String.compareTo().

I have compiled the code so that the output is:

Causality,is,a,relationship

It,is,also,called,causation

It,is,about,a,cause,and,its,affect

Now I want to sort that code (lexicographically) so the output is:

Causality,a,is,relationship

It,also,called,causation,is

It,a,about,affect,and,cause,is,its

However I am generating some crazy output.

My code is below.

Any help would be appreciated.

I have worked on this probably very simple problem for hours and I am ready to destroy my computer. Thanks

public class Wk5Q5 {

    void process1 () {

        String s1 = "Causality is a relationship";
        String s2 = "It is also called causation";
        String s3 = "It is about a cause and its affect";


        ArrayList<String[]> list = new ArrayList<String[]>();


        String[] arr1 = s1.split(" ");
        list.add(arr1);
        String[] arr2 = s2.split(" ");
        list.add(arr2);
        String[] arr3 = s3.split(" ");
        list.add(arr3);

        /**
         * previously sorted the arraylist of string arrays so that
         * each word is separated by commas
         */
        for(int i = 0; i < list.size(); i++){
            for (int j = 0; j < list.get(i).length; j++){
                String t = list.get(i)[j];

                if (j > 0){
                    t = ", " + t;   
                }
                System.out.print(t);
                //System.out.println(list.get(i)[j]);

            }
            System.out.println();
        }

        /**
         * my attempt at sorting each string in each list 
         */
        for(int z = 0; z < list.size(); z++){
            for(int i = 0; i < list.get(z).length; i++){
                String x = list.get(z)[i];
                for (int j = i+1; j < list.get(z).length; j++){
                    String y = list.get(z)[j];
                    if(y.compareTo(x) < 0) {
                        String temp = list.get(z)[i];
                        x = list.get(z)[j];
                        y = temp;
                    }
                    System.out.print(x);
                }

            }
        }
    }
4
  • 5
    What about Collections.sort(myList)? Commented Aug 24, 2012 at 14:47
  • 6
    @sp00m: this is a typical homework question. I don't think that the OP is allowed to use the standard API. user1622888, if this is indeed homework, you should really explicitly mention it in this (and all future) questions. Commented Aug 24, 2012 at 14:49
  • Work on a single sentence first, rather than on a list of sentences. Once you'll get your sort algorithm, it will be easy to apply it on the list. Commented Aug 24, 2012 at 14:53
  • Split the code into peaces. Create methods, attributes of the class. Commented Aug 24, 2012 at 15:23

2 Answers 2

4

The problem with your implementation of the selection sort algorithm is that you do not modify the list being sorted. When you swap x and y, the elements in the corresponding positions of the list remain in their old places.

If you stop using x and y and replace their use with list.get(z)[i] and list.get(z)[j], your sorting algorithm would produce different results. Better yet, if the homework allows you to use standard library, take a look at a built-in way of sorting arrays in Java.

Sign up to request clarification or add additional context in comments.

1 Comment

Or you could use meaningful variable names (to make the code readable) and reassign them to the list ;) Or you could write a method that swaps two elements in a list to impress your teacher.
0

You could use Collections.sort as everyone else is suggesting or do it manually. To do it manually, there are a bunch of different methods. You are going to want to use either a quicksort or a mergesort algorithm. If you don't know what these are, I can explain further; however, if this is HW you will most likely have went over them already

3 Comments

Alright, seriously? Anyone gonna tell me whats wrong with this, or just downvote it?
It's been established that he has to use merge sort and that the Collections library is not an option. It's homework.
@jahroy Where was it established that he had to use a merge sort? I don't see it anywhere. Granted, his code looks like he was trying to do that, but many HW assignments have multiple solutions, I simply provided a different one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.