Sorted Array Push
Using Lodash to implement a sortedPush() array utility function.
HTML
<ul id="sorted"></ul>
<div id="wtf"/>
JavaScript
// Sample array.
var array = [ 2, 3, 5, 7, 11, 13, 19, 23, 29 ];
// Utility function used to push data into the
// array while maintaining the sort order.
function sortedPush( array, value ) {
array.splice( _.sortedIndex( array, value ), 0, value );
}
// "17" gets played into the proper array position.
sortedPush( array, 17 );
// The ul for output.
var list = document.getElementById( "sorted" );
// Display the sorted array.
_.each( array, function( item ) {
var li = document.createElement( "li" );
li.appendChild( document.createTextNode( item ) );
list.appendChild( li );
});
document.getElementById( "wtf" )