|
-
Dec 21st, 2011, 04:59 PM
#1
Thread Starter
Fanatic Member
javascript order by?
I'm trying to sort these "boxes" by subject, or content (line 4 and 5 in the code) ASC or DESC, but I cannot use PHP, it has to be JavaScript (JQuery preferably)... the problem is, I have no clue on where to start, any references or code examples would be much appreciated.
these are the boxes, how they are formatted:
HTML Code:
<div class="box container">
<a href=""><div class="box image"></div></a>
<div class="box text">
<div class="box subject">1 subject</div>
<div class="box content">4 some content here</div>
</div>
</div>
<div class="box container">
<a href=""><div class="box image"></div></a>
<div class="box text">
<div class="box subject">2</div>
<div class="box content">1</div>
</div>
</div>
<div class="box container">
<a href=""><div class="box image"></div></a>
<div class="box text">
<div class="box subject">3 subject</div>
<div class="box content">2 some content here</div>
</div>
</div>
<div class="box container">
<a href=""><div class="box image"></div></a>
<div class="box text">
<div class="box subject">3</div>
<div class="box content">3</div>
</div>
</div>
Thanks in advance,
-
Dec 21st, 2011, 08:09 PM
#2
Re: javascript order by?
Off-the-top-of-my-head idea: select all relevant elements (either $('.subject') or $('.content')) and iterate (.each()) to create two arrays: one that has all the text() values of each element, and one that is associative and has the same text() values for keys, and a corresponding index number for values. Then sort() the first array (followed by reverse() if needed). Iterate the sorted array, using its values as keys for the second (unsorted) array, to get the index of each element to display in order.
Quite probably there's a more efficient method...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|