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;








-1
















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:



enter image description here










share|improve this 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 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

















-1
















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:



enter image description here










share|improve this 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 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













-1












-1








-1









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:



enter image description here










share|improve this question

















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:



enter image description here





This question already has an answer here:



  • How can I swap two values of an array in c#?

    5 answers







c# arrays sorting object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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 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







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












1 Answer
1






active

oldest

votes


















2














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;






share|improve this answer

























  • Thank you so much!

    – user11131093
    Mar 22 at 10:12











  • @user11131093, you are welcome

    – Roman Doskoch
    Mar 22 at 10:13

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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;






share|improve this answer

























  • Thank you so much!

    – user11131093
    Mar 22 at 10:12











  • @user11131093, you are welcome

    – Roman Doskoch
    Mar 22 at 10:13















2














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;






share|improve this answer

























  • Thank you so much!

    – user11131093
    Mar 22 at 10:12











  • @user11131093, you are welcome

    – Roman Doskoch
    Mar 22 at 10:13













2












2








2







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;






share|improve this answer















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;







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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





Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해