Created
June 2, 2009 15:37
-
-
Save pnomolos/122306 to your computer and use it in GitHub Desktop.
This file contains 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.sortOptions = function(ascending) | |
{ | |
var _sortFunc; | |
if ( typeof(ascending) == "function") { | |
_sortFunc = ascending; | |
} else if (!!ascending) { | |
_sortFunc = function(a,b) { | |
return a.text < b.text ? -1 : 1; | |
} | |
} else { | |
_sortFunc = function(a,b) { | |
return a.text < b.text ? 1 : -1; | |
} | |
} | |
function qsort(array, begin, end) | |
{ | |
if(end>begin) { | |
var pivot=begin; | |
pivot=partition(array, begin, end, pivot); | |
qsort(array, begin, pivot-1); | |
qsort(array, pivot+1, end); | |
} | |
} | |
function partition(array, begin, end, pivot) | |
{ | |
var piv=array[pivot]; | |
array.swap(pivot, end); | |
var store=begin; | |
var ix; | |
for(ix=begin; ix<end; ++ix) { | |
if(_sortFunc(array[ix],piv) == -1) { | |
array.swap(store, ix); | |
++store; | |
} | |
} | |
array.swap(end, store); | |
return store; | |
} | |
var arr = this.find('option').get(); | |
qsort(arr,0,this.find('option').size()-1); | |
this.empty(); | |
this.append(arr); | |
this.val(''); | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment