I would check on each sort to see if the whole list was in order. Just checking the last element dosen't seem enough.
$("#sortable").sortable({
stop: function( event, ui ) {
var inorder = true,
$kids = $(ui.item).parent().children(),
current = null;
$($kids).each(function() {
if (current) {
if ($(this).text() < current) {
inorder = false;
return;
}
}
current = $(this).text();
});
if (inorder) alert('Yippee');
}
});
$.fn.randomize = function (childElem) {
return this.each(function () {
var $this = $(this);
var elems = $this.children(childElem);
elems.sort(function () {
return (Math.round(Math.random()) - 0.5);
});
$this.remove(childElem);
for (var i = 0; i < elems.length; i++)
$this.append(elems[i]);
});
}
$(function () {
$('#sortable').randomize('.ui-state-default');
});