# Javascript sort an array by biggest and lowest number

I need help with the sorting as i am totally confused what to do. This is the example:

[1, 65, 3, 52, 48, 63, 31, -3, 18, 56] to [-3, 65, 1, 63, 3, 56, 18, 52, 31, 48].
The first element has to be the smallest, the second to be biggest, the third to be the second smallest element, the fourth to be the second biggest element.

What i have tried is:

``````function solve(arr) {
var lowestNum = [...arr].sort((a,b) => a-b);
let biggestNum = [...arr].sort((a,b) => b-a);
let nums = [];
let i = 0;
while (nums.length !== arr.length) {
nums.push(lowestNum.shift()); lowestNum.pop();
nums.push(biggestNum.shift()); biggestNum.pop();
i++;
}
return nums;
}
console.log(solve([1, 65, 3, 52, 48, 63, 31, -3, 18, 56]));``````

However, that is not the way as my exercise requires me to sort.

### 311 thoughts on “Javascript sort an array by biggest and lowest number”

1. This isnt the most elegant solution, but:

``````let a = [11, 2, 3, -22, 41];
function bigSmallSorter(arr) {
// Sort a copy of the passed in array from least to most.
let sortedArr = [...arr.sort((n, k) => n - k)];
let newArr = [];
let flip = true;

for( let i of arr ) {
let temp = flip
// Take first element of the sorted array
? sortedArr.shift()

// Take the last element of the sorted array
: sortedArr.pop();

// Add the newest value to the value that is ultimately returned
newArr.push(temp);

// Flip this so the next iteration takes from the other
// end of the array
flip = !flip;
}

// Profit
return newArr;
}

console.log(bigSmallSorter(a))

//>[ -22, 41, 2, 11, 3 ]
``````

And an version of the method without comments:

``````let a = [11, 2, 3, -22, 41];

function bigSmallSorter(arr) {
let sortedArr = [...arr.sort((n, k) => n - k)]
let newArr = [];
let flip = true;

for( let i of arr ) {
let temp = flip
? sortedArr.shift()
: sortedArr.pop()
newArr.push(temp)
flip = !flip;
}

return newArr;
}

console.log(bigSmallSorter(a))``````

6. [url=http://essaysrv.com/]good college application essay[/url]

7. [url=https://sildenafilextra.com/]where to order viagra[/url] [url=https://viagrabasic.com/]sildenafil 100mg tablets buy online[/url] [url=https://gmipharmacy.com/]best value pharmacy[/url] [url=https://onlimepharmacy.com/]reputable indian pharmacies[/url] [url=https://canadianapharmacy.com/]medication canadian pharmacy[/url]

8. [url=https://viagrayep.com/]viagra tablet online purchase[/url]

10. [url=https://dbcialis.com/]order real cialis[/url] [url=https://paxilssri.com/]how much is paxil tablet[/url] [url=https://suprpharm.com/]bupropion india[/url] [url=https://ktpills.com/]lexapro 10 mg price[/url]

12. [url=http://cialisdi.com/]dapoxetine 120 mg[/url] [url=http://viagradx.com/]buy online viagra uk[/url] [url=http://viagra21.com/]buy cheap viagra online usa[/url] [url=http://azzmeds.com/]tadalafil 5mg tablets price[/url] [url=http://rxphealth.com/]sildenafil brand name in india[/url] [url=http://viagrapoint.com/]cheapest sildenafil tablets[/url]