Order an array with bubble sort

Hello guys! does anyone know how to solve this? Thank you very much!!

Order an array of numbers with bubble sort

New array must be returned

The algorithm is going to receive an array of objects like this:

{

name: "Notebook",

price: 1200,

review: 8

}

this objects should be sorted in the way the next parameters indicate “firstOrd”, “secondOrd”

secondOrd is used in the cases firstOrd have the same value

var array = [

{name: "Notebook", price: 1200, review: 8},

{name: "Smartphone", price: 300, review: 9},

{name: "TV", price: 700, review: 1},

{name: "PS5", price: 1100, review: 7}

]

First example

magicSort(array, "price") should be:

[

{name: "Smartphone", price: 300, review: 9},

{name: "TV", price: 800, review: 1},

{name: "Notebook", price: 1200, review: 8}

{name: "PS5", price: 1200, review: 7}

]

2nd example

magicSort(array, "price", "review") should be

[

{name: "Smartphone", price: 300, review: 9},

{name: "TV", price: 800, review: 1},

{name: "PS5", price: 1200, review: 7},

{name: "Notebook", price: 1200, review: 8}

]
function magicSort(array, firstOrd, secondOrd){ }

What have you tried so far?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Do you understand the concept of bubble sort? If not then I would google ‘bubble sort’ to get an idea of how it works. If so, then put it into code the best you can and then you can show us what you have done and we can offer suggestions.

lol

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You can use destructuring to avoid that aux variable by doing this instead [array[i] , array [i+1]] = [array[i + 1] , array [i]]

To make this easier on yourself, I would first create a bubble sort for just an array of numbers (not objects like the actual instructions give). Get the bubble sort to work with the array of numbers so you have the basic logic down. Then you can expand it to work with an array of objects. This won’t be that much harder to do, but until you have the basic sort logic down I think trying to take into account the added complexity of comparing objects will just get in the way.

2 Likes

That’s not a bubble-sort algorithm though.
Also why implement a while-loop, if you code it so it runs exactly one time?

For a start, look up how bubble sort works.
Then implement it on a testing array to understand it.
Then write it for firstOrd.
Then think about how to deal with secondOrd.

Ok guys but im looking for the answer in code since I need to use it to study, cant find anything online please help me!!

We do not solve homework questions for users. We can help you fix your code though.

No, you should be looking for a description of how bubble sort works and then you create the code based on your understanding. Bubble sort is easy enough that you should be able to implement it rather easily once you understand how it works. I’m confident you can do it.

You should be able to easily find a ton of explanations with a simple search for bubblesort but here is one I found that explains it quite well: Standard sorting algorithms - Searching and sorting algorithms - OCR - GCSE Computer Science Revision - OCR - BBC Bitesize

Sorry, but what are you talking about?!
It’s such a well known algorithm, it’s even explained on wikipedia: Bubble sort - Wikipedia

Pretty sure you could enter the name into google and would find thousands of code examples.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.