How to bubble sort objects in array? [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How can I swap two values of an array in c#?What is the most efficient way to deep clone an object in JavaScript?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I sort a dictionary by value?Sorting an array of JavaScript objects by propertyChecking if a key exists in a JavaScript object?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Using audio cues to encourage good posture
Take 2! Is this homebrew Lady of Pain warlock patron balanced?
Dating a Former Employee
Do any jurisdictions seriously consider reclassifying social media websites as publishers?
Why does the remaining Rebel fleet at the end of Rogue One seem dramatically larger than the one in A New Hope?
Why is it faster to reheat something than it is to cook it?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Is CEO the "profession" with the most psychopaths?
How do I use the new nonlinear finite element in Mathematica 12 for this equation?
SF book about people trapped in a series of worlds they imagine
What was the first language to use conditional keywords?
Can anything be seen from the center of the Boötes void? How dark would it be?
How would a mousetrap for use in space work?
Why wasn't DOSKEY integrated with COMMAND.COM?
What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?
Why aren't air breathing engines used as small first stages?
What is the font for "b" letter?
Is there a kind of relay only consumes power when switching?
Multiple OR (||) Conditions in If Statement
What does it mean that physics no longer uses mechanical models to describe phenomena?
What do you call the main part of a joke?
Project Euler #1 in C++
What would you call this weird metallic apparatus that allows you to lift people?
Localisation of Category
How to bubble sort objects in array? [duplicate]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How can I swap two values of an array in c#?What is the most efficient way to deep clone an object in JavaScript?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I sort a dictionary by value?Sorting an array of JavaScript objects by propertyChecking if a key exists in a JavaScript object?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
How can I swap two values of an array in c#?
5 answers
I have a bit a of a problem. I'm trying to sort an array with objects in it. The objects are bottles with names, prices and types. The user makes a choice, which bottle he/she want's to add to the array.
For the assignment we have to use bubble sort. I've made it work, except it only sorts the price. The entire object doesn't switch place just the price itself. So if Coca-Cola's original price was 13 in list, after bubble sort it's 10. So the only thing that changes or gets sorted is the price and not the entire object, if that makes any sense.
public void sort_sodas()
{
int max = sodas.Length - 1;
for (int i = 0; i < max; i++)
int nrLeft = max - i;
for (int j = 0; j < nrLeft; j++)
if (sodas[j+1] == null)
break;
else if (sodas[j].Price > sodas[j+1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
Below an image of before and after bubble sort:

c# arrays sorting object
marked as duplicate by Uwe Keim, Selvin, Community♦ Mar 22 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I swap two values of an array in c#?
5 answers
I have a bit a of a problem. I'm trying to sort an array with objects in it. The objects are bottles with names, prices and types. The user makes a choice, which bottle he/she want's to add to the array.
For the assignment we have to use bubble sort. I've made it work, except it only sorts the price. The entire object doesn't switch place just the price itself. So if Coca-Cola's original price was 13 in list, after bubble sort it's 10. So the only thing that changes or gets sorted is the price and not the entire object, if that makes any sense.
public void sort_sodas()
{
int max = sodas.Length - 1;
for (int i = 0; i < max; i++)
int nrLeft = max - i;
for (int j = 0; j < nrLeft; j++)
if (sodas[j+1] == null)
break;
else if (sodas[j].Price > sodas[j+1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
Below an image of before and after bubble sort:

c# arrays sorting object
marked as duplicate by Uwe Keim, Selvin, Community♦ Mar 22 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
obviously you should swap whole object not only price ..var temp = sodas[j]; sodas[j] = ...and so on
– Selvin
Mar 22 at 10:05
2
But your code only changes thePrice. What type of objects are in thesodas? Can you swap whole objects instead?
– doctorlove
Mar 22 at 10:06
I've tried to swap the entire object and not just the price. I cant make it work, if anyone could show me how it's done, I would be grateful. thank you!
– user11131093
Mar 22 at 10:09
add a comment |
This question already has an answer here:
How can I swap two values of an array in c#?
5 answers
I have a bit a of a problem. I'm trying to sort an array with objects in it. The objects are bottles with names, prices and types. The user makes a choice, which bottle he/she want's to add to the array.
For the assignment we have to use bubble sort. I've made it work, except it only sorts the price. The entire object doesn't switch place just the price itself. So if Coca-Cola's original price was 13 in list, after bubble sort it's 10. So the only thing that changes or gets sorted is the price and not the entire object, if that makes any sense.
public void sort_sodas()
{
int max = sodas.Length - 1;
for (int i = 0; i < max; i++)
int nrLeft = max - i;
for (int j = 0; j < nrLeft; j++)
if (sodas[j+1] == null)
break;
else if (sodas[j].Price > sodas[j+1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
Below an image of before and after bubble sort:

c# arrays sorting object
This question already has an answer here:
How can I swap two values of an array in c#?
5 answers
I have a bit a of a problem. I'm trying to sort an array with objects in it. The objects are bottles with names, prices and types. The user makes a choice, which bottle he/she want's to add to the array.
For the assignment we have to use bubble sort. I've made it work, except it only sorts the price. The entire object doesn't switch place just the price itself. So if Coca-Cola's original price was 13 in list, after bubble sort it's 10. So the only thing that changes or gets sorted is the price and not the entire object, if that makes any sense.
public void sort_sodas()
{
int max = sodas.Length - 1;
for (int i = 0; i < max; i++)
int nrLeft = max - i;
for (int j = 0; j < nrLeft; j++)
if (sodas[j+1] == null)
break;
else if (sodas[j].Price > sodas[j+1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
Below an image of before and after bubble sort:

This question already has an answer here:
How can I swap two values of an array in c#?
5 answers
c# arrays sorting object
c# arrays sorting object
edited Mar 22 at 10:08
Ipsit Gaur
2,31411429
2,31411429
asked Mar 22 at 10:04
user11131093user11131093
186
186
marked as duplicate by Uwe Keim, Selvin, Community♦ Mar 22 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Uwe Keim, Selvin, Community♦ Mar 22 at 10:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
obviously you should swap whole object not only price ..var temp = sodas[j]; sodas[j] = ...and so on
– Selvin
Mar 22 at 10:05
2
But your code only changes thePrice. What type of objects are in thesodas? Can you swap whole objects instead?
– doctorlove
Mar 22 at 10:06
I've tried to swap the entire object and not just the price. I cant make it work, if anyone could show me how it's done, I would be grateful. thank you!
– user11131093
Mar 22 at 10:09
add a comment |
4
obviously you should swap whole object not only price ..var temp = sodas[j]; sodas[j] = ...and so on
– Selvin
Mar 22 at 10:05
2
But your code only changes thePrice. What type of objects are in thesodas? Can you swap whole objects instead?
– doctorlove
Mar 22 at 10:06
I've tried to swap the entire object and not just the price. I cant make it work, if anyone could show me how it's done, I would be grateful. thank you!
– user11131093
Mar 22 at 10:09
4
4
obviously you should swap whole object not only price ..
var temp = sodas[j]; sodas[j] = ... and so on– Selvin
Mar 22 at 10:05
obviously you should swap whole object not only price ..
var temp = sodas[j]; sodas[j] = ... and so on– Selvin
Mar 22 at 10:05
2
2
But your code only changes the
Price. What type of objects are in the sodas? Can you swap whole objects instead?– doctorlove
Mar 22 at 10:06
But your code only changes the
Price. What type of objects are in the sodas? Can you swap whole objects instead?– doctorlove
Mar 22 at 10:06
I've tried to swap the entire object and not just the price. I cant make it work, if anyone could show me how it's done, I would be grateful. thank you!
– user11131093
Mar 22 at 10:09
I've tried to swap the entire object and not just the price. I cant make it work, if anyone could show me how it's done, I would be grateful. thank you!
– user11131093
Mar 22 at 10:09
add a comment |
1 Answer
1
active
oldest
votes
You should not change prices of objects here:
else if (sodas[j].Price > sodas[j + 1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
You should change object positions:
else if (sodas[j].Price > sodas[j + 1].Price)
var tempObject = sodas[j];
sodas[j] = sodas[j + 1];
sodas[j + 1] = tempObject;
Thank you so much!
– user11131093
Mar 22 at 10:12
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should not change prices of objects here:
else if (sodas[j].Price > sodas[j + 1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
You should change object positions:
else if (sodas[j].Price > sodas[j + 1].Price)
var tempObject = sodas[j];
sodas[j] = sodas[j + 1];
sodas[j + 1] = tempObject;
Thank you so much!
– user11131093
Mar 22 at 10:12
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
add a comment |
You should not change prices of objects here:
else if (sodas[j].Price > sodas[j + 1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
You should change object positions:
else if (sodas[j].Price > sodas[j + 1].Price)
var tempObject = sodas[j];
sodas[j] = sodas[j + 1];
sodas[j + 1] = tempObject;
Thank you so much!
– user11131093
Mar 22 at 10:12
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
add a comment |
You should not change prices of objects here:
else if (sodas[j].Price > sodas[j + 1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
You should change object positions:
else if (sodas[j].Price > sodas[j + 1].Price)
var tempObject = sodas[j];
sodas[j] = sodas[j + 1];
sodas[j + 1] = tempObject;
You should not change prices of objects here:
else if (sodas[j].Price > sodas[j + 1].Price)
int temp = sodas[j].Price;
sodas[j].Price = sodas[j + 1].Price;
sodas[j + 1].Price = temp;
You should change object positions:
else if (sodas[j].Price > sodas[j + 1].Price)
var tempObject = sodas[j];
sodas[j] = sodas[j + 1];
sodas[j + 1] = tempObject;
edited Mar 22 at 10:10
answered Mar 22 at 10:09
Roman DoskochRoman Doskoch
9,276102738
9,276102738
Thank you so much!
– user11131093
Mar 22 at 10:12
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
add a comment |
Thank you so much!
– user11131093
Mar 22 at 10:12
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
Thank you so much!
– user11131093
Mar 22 at 10:12
Thank you so much!
– user11131093
Mar 22 at 10:12
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
@user11131093, you are welcome
– Roman Doskoch
Mar 22 at 10:13
add a comment |
4
obviously you should swap whole object not only price ..
var temp = sodas[j]; sodas[j] = ...and so on– Selvin
Mar 22 at 10:05
2
But your code only changes the
Price. What type of objects are in thesodas? Can you swap whole objects instead?– doctorlove
Mar 22 at 10:06
I've tried to swap the entire object and not just the price. I cant make it work, if anyone could show me how it's done, I would be grateful. thank you!
– user11131093
Mar 22 at 10:09