This is the third of a series of short posts in which I solve interview questions from my Professional Practices course using Ruby.

The Prompt

Sort the following array in numeric order:

var array1 = [1, 30, 4, 21, 100000];

The Solution

With Ruby

Ruby understands that you would want to sort an array of numbers in increasing order.

array1 = [1, 30, 4, 21, 100000]
array1.sort!

With JavaScript

In JavaScript, a sort function must be provided, otherwise it will not sort in numeric order.

var array1 = [1, 30, 4, 21, 100000];
array1.sort((x, y) => x - y);