SciActive

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

jQuery Element Sorting

E-mail Print PDF
User Rating: / 0
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

B
i
u
Quote
Code
List
List item
URL
Name *
Email (For verification & Replies)
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment
 

Newsflashes

2009-05-14
Dandelion 0.11 Alpha released. This is the second public Dandelion release.
 
2009-04-15
SciActive Forums are now available! Register for free today.
 

Advertisement