Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
JavaScript Bubble sort for objects in an array
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list and swapping adjacent elements if they are in the wrong order. When applied to an array of objects, we can sort based on specific properties.
The Shoe Class
Let's start with a constructor class that creates Shoe objects:
class Shoe {
constructor(name, price, type) {
this.name = name;
this.price = price;
this.type = type;
}
}
Creating the Array of Objects
We'll create an array of shoe objects with different prices:
const arr = [
new Shoe('Nike AirMax 90', '120', 'Casual'),
new Shoe('Jordan Retro 1', '110', 'Casual'),
new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
new Shoe('Adidas X Ghosted', '110', 'Athletic'),
new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
new Shoe('Aldo Loafers', '130', 'Formal'),
new Shoe('Timberlands', '199', 'Seasonal boots'),
new Shoe('Converse High Tops', '70', 'Casual'),
new Shoe('Converse Low Tops', '80', 'Casual'),
new Shoe('Adidas NMDs', '110', 'Athletic'),
new Shoe('Heels', '130', 'Formal'),
new Shoe('Nike AirForce', '150', 'Casual')
];
console.log("Original array:");
arr.forEach(shoe => console.log(`${shoe.name}: $${shoe.price}`));
Original array: Nike AirMax 90: $120 Jordan Retro 1: $110 Jadon Doc Martens: $250 Adidas X Ghosted: $110 Nike Vapourmax Flyknit: $250 Aldo Loafers: $130 Timberlands: $199 Converse High Tops: $70 Converse Low Tops: $80 Adidas NMDs: $110 Heels: $130 Nike AirForce: $150
Bubble Sort Implementation
Here's the bubble sort function that sorts objects by their price property:
class Shoe {
constructor(name, price, type) {
this.name = name;
this.price = price;
this.type = type;
}
}
const arr = [
new Shoe('Nike AirMax 90', '120', 'Casual'),
new Shoe('Jordan Retro 1', '110', 'Casual'),
new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
new Shoe('Adidas X Ghosted', '110', 'Athletic'),
new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
new Shoe('Aldo Loafers', '130', 'Formal'),
new Shoe('Timberlands', '199', 'Seasonal boots'),
new Shoe('Converse High Tops', '70', 'Casual'),
new Shoe('Converse Low Tops', '80', 'Casual'),
new Shoe('Adidas NMDs', '110', 'Athletic'),
new Shoe('Heels', '130', 'Formal'),
new Shoe('Nike AirForce', '150', 'Casual')
];
const bubbleSort = (arr = []) => {
let swapped;
do {
swapped = false;
for (let i = 0; i +arr[i + 1].price) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
};
bubbleSort(arr);
console.log("Sorted array by price:");
arr.forEach(shoe => console.log(`${shoe.name}: $${shoe.price}`));
Sorted array by price: Converse High Tops: $70 Converse Low Tops: $80 Jordan Retro 1: $110 Adidas X Ghosted: $110 Adidas NMDs: $110 Nike AirMax 90: $120 Aldo Loafers: $130 Heels: $130 Nike AirForce: $150 Timberlands: $199 Jadon Doc Martens: $250 Nike Vapourmax Flyknit: $250
How the Algorithm Works
The bubble sort algorithm works by:
- Comparing adjacent elements in the array
- Swapping them if they're in the wrong order (higher price before lower price)
- Using the unary plus operator (+) to convert string prices to numbers for comparison
- Repeating until no more swaps are needed
Key Points
-
String to Number Conversion: The
+arr[i].priceconverts the string price to a number for proper numerical comparison - Do-While Loop: Ensures at least one pass through the array
- Swapped Flag: Tracks whether any swaps occurred to determine when sorting is complete
- Time Complexity: O(n²) in worst case, O(n) in best case when array is already sorted
Conclusion
Bubble sort provides a straightforward way to sort arrays of objects by their properties. While not the most efficient for large datasets, it's easy to understand and implement for sorting objects by any comparable property.
Advertisements
