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