Skip to content

Instantly share code, notes, and snippets.

@fgrehm
Created December 10, 2012 17:11
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 fgrehm/4251894 to your computer and use it in GitHub Desktop.
Save fgrehm/4251894 to your computer and use it in GitHub Desktop.
Inserts a new element in order
$.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