Results 1 to 2 of 2

Thread: javascript order by?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    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,

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    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
  •  



Click Here to Expand Forum to Full Width