Created
December 10, 2012 17:11
-
-
Save fgrehm/4251894 to your computer and use it in GitHub Desktop.
Inserts a new element in order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.insertInOrder = (newElement, iterator) -> | |
$container = $(this) | |
$siblings = $container.children() | |
$newElement = $(newElement) | |
if $siblings.length == 0 | |
$container.append($newElement) | |
return this | |
index = _.sortedIndex $siblings, newElement, iterator | |
if index == $siblings.length | |
$container.append $newElement | |
else | |
$($siblings[index]).before($newElement) | |
this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment