3

now I have been at this for a while and there is an error I am having. Now the program I am making is an address book, and I am using an insertion sort to sort an arraylist of objects which I call books(address entries). Now I soon discovered that my sorter is not sorting properly so I made a simple program to test the sorter and again it does not work. I was wondering if you guys could have a look at it and help me out.

Here is my Sorter:

import java.util.ArrayList;
public class Sorts {

    /**
     * Sorts and array of integer from low to high
     * pre: none
     * post: Integers has been sorted from low to high
     */
    public static void insertionSort(ArrayList<String> test) {
        Comparable temp;
        int previousIndex;
        ArrayList<String> objectSort = test;

        for (int i = 1; i < objectSort.size(); i++) {
            temp = objectSort.get(i);
            previousIndex = i - 1;

            while ((objectSort.get(previousIndex).compareTo((String) temp)) == 1 && (previousIndex > 0)) {
                objectSort.set(previousIndex + 1, objectSort.get(previousIndex));
                previousIndex -= 1; //decrease index to compare current item with next previous item
            }
            if (objectSort.get(previousIndex).compareTo((String) temp) == 1) {
                /* shift item in first element up into next element */
                objectSort.set(previousIndex + 1, objectSort.get(previousIndex));
                /* place current item at index 0 (first element */
                objectSort.set(previousIndex, (String) temp);
            } else {
                /* place current item at index ahead of previous item */
                objectSort.set(previousIndex + 1, (String) temp);
            }

        }
    }
}

My simple program to test it is:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args){
        ArrayList<String> test = new ArrayList<String>();

        test.add("Roxy");
        test.add("Proxy");
        test.add("Moxy");
        test.add("Samuel Adams");

        Sorts.insertionSort(test);

        System.out.println(test);

    }

}

To sum it up, I am having troubles with my ArrayList sorter. The problem is it wont sort correctly and I do not know why. Thank you so much in advance. If you have any questions feel free to ask. :)

3
  • Can you please show expected output and actual output? Commented Jun 18, 2012 at 21:47
  • 3
    Any reason why you don't use Collections.sort(test);? Commented Jun 18, 2012 at 21:47
  • This is for a school assignment. I choose to make an address book. One of the prerequisites is to use a sorter or a finder, however they need to be custom made, I cant use classes that are included in java. I used to use the Insertion sorter for an array and it worked perfectly. However I decided to use an arraylist to make things more universal and efficient in my program. Commented Jun 18, 2012 at 21:51

1 Answer 1

8

First problem: you're expecting compareTo to always return 1 for "greater than". It just returns a value greater than 0 which may be a different positive integer. So both your == 1 comparisons should be > 0.

There may be other problems, but that's the first one I'd look at.

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

2 Comments

Hey, thank you so much Jon. It works perfectly. I will click accept on your answer once 5 minutes is up. But again, thank you so much. :)
This was a life saver for my project also! Thank you!

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.