Skip to content

Instantly share code, notes, and snippets.

@boy3vil
Created July 27, 2013 11:31
Show Gist options
  • Save boy3vil/6094611 to your computer and use it in GitHub Desktop.
Save boy3vil/6094611 to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function() {
var options = $('select.whatever option');
var arr = options.map(function(_, o) {
return {
t: $(o).text(),
v: o.value
};
}).get();
arr.sort(function(o1, o2) {
return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0;
});
options.each(function(i, o) {
console.log(i);
o.value = arr[i].v;
$(o).text(arr[i].t);
});
});
});
</script>
</head>
<body>
<button type=button>Sort Options</button>
<select class='whatever'>
<option value='22'>Hello</option>
<option value='101'>Banana</option>
<option value='1'>Sugar Cane</option>
<option value='-2'>Palm Oil</option>
</select>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment