Skip to content

Instantly share code, notes, and snippets.

@alvinsj
Created March 9, 2012 09:33
Show Gist options
  • Save alvinsj/2005841 to your computer and use it in GitHub Desktop.
Save alvinsj/2005841 to your computer and use it in GitHub Desktop.
[jquery] sort then group by character
$.sort_then_group_by_character = (sortable, ul_class)->
$.sort_by_words(ul_class, sortable)
$('ul.group').empty()
count = 0
character = ''
$("ul.#{ul_class} li").each (index, li_el) ->
# get attributes
str = $(li_el).data(sortable).toLowerCase()
content = $(li_el).html()
# reset element when letter change
if character isnt str[0]
character = null
# new character, build box
unless character
character = str[0]
$('ul.group').append("<li id='box-#{character}' class='group-item'><div class='outer'><div class='title'>#{character.toUpperCase()}</div><ul class='subgroup'></li>")
# prepare item
item = $("<a id='item-#{character}'>#{content}</a>")
$.each item.data(), (k,v) ->
item.data(k,$(li_el).data(k))
# wrap item
el = $("<li class='subgroup'></li>").append(item)
# add item
$("li#box-#{character} ul.subgroup").append(el)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment