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

  • Home
  • SEARCH
  • 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 6800845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:01:15+00:00 2026-05-26T19:01:15+00:00

I have been working on this for the past 5 hours. For some reason

  • 0

I have been working on this for the past 5 hours. For some reason I am not getting the result I am looking for. The method is supposed to sort an ArrayList of items by quantity using bubble sort. Not sure if I am making a mistake but it seems to only sort the first few items and just lists the rest as they are and not in order.
Here is the code:

public static void bubblesrt(ArrayList<Drinks> list)
{
    Drink temp;
    if (list.size()>1) // check if the number of orders is larger than 1
    {
        for (int x=0; x<list.size(); x++) // bubble sort outer loop
        {
            for (int i=0; i < list.size()-i; i++) {
                if (list.get(i).compareTo(list.get(i+1)) > 0)
                {
                    temp = list.get(i);
                    list.set(i,list.get(i+1) );
                    list.set(i+1, temp);
                }
            }
        }
    }

}

and this is the compareTo() method which is located in a Drinks class

public int compareTo(Drinks z) 
{
    int res=0;
    if (quantity < z.quantity) {res = -1;}
    if (quantity > z.quantity) {res = 1;}
    return res;
}

How do I make this work as intended?

  • 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-05-26T19:01:16+00:00Added an answer on May 26, 2026 at 7:01 pm

    Try changing

    list.add(i,list.get(i+1) );
    

    to

    list.set(i,list.get(i+1) );
    

    and

    for (int i=0; i < list.size()-i; i++)
    

    to

    for (int i=0; i < list.size() - x - 1; i++)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this for the past couple of hours, and searching
Have been working on this question for a couple hours and have come close
I have been working on this for 24 hours now, trying to optimize it.
I have been working on this for some time now and just cant quite
i have been working on this sub-menu for 8 hours now, the last little
I have been working on this for some time. The script does work and
I've been working away at this problem for the past 10-12 hours , and
I've been working on this for the past several days and have been stuck.
I've been working on this for the past 11 hours and I think I've
I've been working on this for the past few hours, and after going through

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.