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 8993401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:11:13+00:00 2026-06-15T23:11:13+00:00

I want to sort an ArrayList so that it is smallest to largest. I

  • 0

I want to sort an ArrayList so that it is smallest to largest. I have the following code:

public static ArrayList<BigInteger> sortBigInteger(ArrayList<BigInteger> toSort){
    ArrayList<BigInteger> toReturn = new ArrayList<BigInteger>();
    toReturn.add(toSort.remove(0));
    for (int i = 0; i < toSort.size(); i++){
        BigInteger n = toSort.get(i);
        boolean eval = false;
        in: for (int a = 0; a < toReturn.size(); a++){
            if (n.compareTo(toReturn.get(a)) < 0){
                toReturn.add(a, toSort.remove(i));
                eval = true;
                break in;
            }
        }
        if (!eval) toReturn.add(toSort.remove(i));
    }
    toSort = toReturn;
    return toReturn;
}

However, I lose elements. With an ArrayList of size 32, I get an ArrayList of size 15. Where are the extra removes happening?
Main Question: How do you sort an ArrayList?

  • 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-15T23:11:15+00:00Added an answer on June 15, 2026 at 11:11 pm

    You can always use Collections.sort(toSort)1.

    It will simply sort the ArrayList toSort.


    Note – it is usually much better to use built in libraries, that are already built tested and optimized for you, rather then re-invent the wheel. The result in most cases is likely to :

    • More efficient
    • More correct (already taken care of some nasty edge cases you might have missed)
    • Faster to develop
    • Better maintained

    P.S.

    The reason your algorithm fails is that you are keep removing elements from toSort, and then read from larger indices.

    For a short example, have a look on the list [5,2,9].

    First you have toSort = [5,2,9] , toReturn = []

    When i == 0, you will basically invoke: toReturn.add(toSort.remove(0));

    It will result in the lists: toSort = [2,9], toReturn = [5].

    But, on next iteration – i==1, and you will check the element with index 1, which is now 9, not 2! You missed an element!

    (This is actually a good example why using a built in sort is usually better!)


    (1) Assuming from your comments you only want to sort it, and not doing it as self improvement or task. If you are interested about sorting algorithms, you might find the wikipedia page on sorting algorithms helpful, especially the one that is actually used in java – TimSort.

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

Sidebar

Related Questions

I want to sort an ArrayList by a property. This is my code... public
I want to sort the items that have been previously selected with checkboxes. If
I searched existing tutorial. Im following that code. I want to show particular list
i want to sort an ArrayList that has the type Node, like this :
I want to store buttons in some sort of collection, arraylist, so that I
I have three array lists with String .I want to sort one arraylist and
I want to sort an arraylist of pairs of integers. So far I've been
I have a string array or arraylist that is passed to my program in
I have 9 different ArrayList and I want to have a list of the
I have a Map<byte[], Element> and I want to sort it and write it

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.