Skip to content

Instantly share code, notes, and snippets.

@KatieK2
Created August 22, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatieK2/6309503 to your computer and use it in GitHub Desktop.
Save KatieK2/6309503 to your computer and use it in GitHub Desktop.
Manual Array Sorter
<h1>Manual Array Sorter</h1>
<p>Without using .sort(), write a function to numerically sort an array of integers. Example: <code>var sortThis[2,3,1,5,4];</code></p>
<h2>h2</h2>

Manual Array Sorter

Interview Question: Without using .sort(), write a function to numerically sort an array of integers.

A Pen by KatieK on CodePen.

License.

var sortThis = [5,2,4,3,1];
var a, b;
for (var j=0; j<sortThis.length; j++)
{ for (var i=0; i<sortThis.length; i++)
{
a = sortThis[i];
b = sortThis[i+1];
if ( a > b && b )
{ sortThis[i] = b; sortThis[i+1] = a; }
// Debug: $("body").append("<p>" + j + "." + i + ": " + sortThis + "</p>");
}
}
$("h2").text(sortThis);
h1, h2, p { margin: 1rem; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment