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
Sorting parts of array separately in JavaScript
We have an array that contains many objects. We are required to write a function to sort the first half of the array in ascending order and the second half of the array in ascending order as well, but without inter-mixing the entries of halves into one another.
Consider this sample array:
const arr = [
{id:1, x: 33},
{id:2, x: 22},
{id:3, x: 11},
{id:4, x: 3},
{id:5, x: 2},
{id:6, x: 1}
];
Our function should sort this array based on the 'x' property of the objects while keeping each half separate.
How It Works
The approach involves splitting the array into two halves, sorting each half independently, and then combining them back together. Here's the step-by-step process:
- Create a copy of the original array to avoid mutation
- Split the array into first half and second half
- Sort each half separately based on the 'x' property
- Combine the sorted halves back together
Example Implementation
const arr = [
{id:1, x: 33},
{id:2, x: 22},
{id:3, x: 11},
{id:4, x: 3},
{id:5, x: 2},
{id:6, x: 1}
];
const sortInParts = array => {
const arr = array.slice(); // Create a copy
const sorter = (a, b) => {
return a['x'] - b['x'];
};
// Split array into two halves
const arr1 = arr.splice(0, arr.length / 2);
// Sort each half independently
arr.sort(sorter);
arr1.sort(sorter);
// Combine sorted halves
return [...arr1, ...arr];
};
console.log(sortInParts(arr));
[
{ id: 3, x: 11 },
{ id: 2, x: 22 },
{ id: 1, x: 33 },
{ id: 6, x: 1 },
{ id: 5, x: 2 },
{ id: 4, x: 3 }
]
Alternative Approach Using slice()
Here's another way to implement the same functionality using slice() instead of splice():
const sortInPartsAlternative = array => {
const midpoint = Math.floor(array.length / 2);
// Get first half and second half using slice
const firstHalf = array.slice(0, midpoint);
const secondHalf = array.slice(midpoint);
const sorter = (a, b) => a.x - b.x;
// Sort each half
const sortedFirstHalf = firstHalf.sort(sorter);
const sortedSecondHalf = secondHalf.sort(sorter);
return [...sortedFirstHalf, ...sortedSecondHalf];
};
const testArray = [
{id:1, x: 33}, {id:2, x: 22}, {id:3, x: 11},
{id:4, x: 3}, {id:5, x: 2}, {id:6, x: 1}
];
console.log(sortInPartsAlternative(testArray));
[
{ id: 3, x: 11 },
{ id: 2, x: 22 },
{ id: 1, x: 33 },
{ id: 6, x: 1 },
{ id: 5, x: 2 },
{ id: 4, x: 3 }
]
Key Points
- The original array is preserved by creating a copy with
slice() - Each half maintains its position in the final result
- Both halves are sorted independently using the same comparison function
- The spread operator
...is used to combine the sorted halves
Conclusion
Sorting array parts separately is useful when you need to maintain data locality while still organizing elements within their respective sections. This technique preserves the logical separation of data while applying sorting within each segment.
