Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:51:31+00:00 2026-06-06T03:51:31+00:00

now I have been at this for a while and there is an error

  • 0

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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:51:34+00:00Added an answer on June 6, 2026 at 3:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have been having trouble with this for a while now. I have
I have been having this issue for a while now and I cannot seem
I have been trying this for a while now and can't get it to
I have been trying this for a good while now but I can't seem
I have been working on this problem for a while now. I am trying
I have been bashing my head against this for a while now and need
So I have been working on this project for a short while now. I
I have been learning python for some time now. While starting this learning python
I have been trying to figure this out for a while now, but every
I've been struggling with this issue for a while now. I have OpenCart 1.5.2.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.