SciActive

  • Increase font size
  • Default font size
  • Decrease font size
Home Articles jQuery Element Sorting

jQuery Element Sorting

E-mail Print PDF
User Rating: / 2
PoorBest 

I recently needed to sort div elements in the DOM by a timestamp in child divs. After searching for a while and finding no jQuery style solution, I figured out an easy way to do it, the jQuery way. Here is the code:

// Sort by timestamp.
$("#timeclock_edit").prepend($("#timeclock_edit div.element").get().sort(function(a, b){
    return $(a).find(".timestamp").text() - $(b).find(".timestamp").text();
}));

The code grabs an array of DOM elements, sorts it by comparing the text in the timestamp element, then prepends that to the parent div. The way this is coded is not very flexible, but it could be expanded easily.

	

Comments

Please login to post comments or replies.
 

Advertisement