[jQuery] Select Box Sorter

[jQuery] Select Box Sorter

I've created a really simple plugin that lets you do $("select").sortSelectBy("html").
It automatically sorts by a jQuery function, so you can create your own and have it sort by that. For instance, I created a getCompany jQuery method below that extracts the company out of "Name (Company)". You could then do $("select").sortSelectBy("getCompany").
<span style="font-family: courier new,monospace;">
jQuery.fn.sortSelectBy = function(sort) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  return this.each
(function(i, self) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    sorted = jQuery("option", this).get().sort(function(a,b) { return (jQuery(a)[sort]() > jQuery(b)[sort]()) ? 1 : -1 })
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    jQuery(this).empty().append(sorted)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
  })</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
jQuery.fn.getCompany = function() {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  company = this.html().match(/\(([^\)]*)\)/)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">  if(company) return company[1];</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br clear="all">
--
Yehuda Katz
Web Developer | Wycats Designs
(ph)  718.877.1325
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/