SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Sorting by text, then numbers

    Hi,

    I have this code working, it's sorting by text, then a timestamp:
    Code JavaScript:
    jQuery.fn.sortBy = function() {  
        var selectors = arguments;
     
        this.sort(function(a, b) {
            // run through each selector, and return first non-zero match
            for(var i = 0; i < selectors.length; i++) {
                var selector = selectors[i];
     
                var first = $(selector, a).text();
                var second = $(selector, b).text();
     
                var isNumeric = Number(first) && Number(second);
                if(isNumeric) {
                    var diff = first - second;
                    if(diff != 0) {
                        return diff;
                    }
                }
                else if(first != second) {
                    return first < second ? -1 : 1;
                }
            }
     
            return 0;
        });
     
        this.appendTo(this.parent());
     
        return this;
    };

    The script is called by the fuction
    Code JavaScript:
    $(".list_item").sortBy('.count', '.timestamp');


    Can someone please help me change the timestamp to sorting by regular number, like 1 - 10?

    Thank you!

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I can't see any mention of timestamps... are you sure this is all the code?

    By the way, you could use jQuery's each() method instead of the for loop.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Hi Raffles,

    I got the jQuery from a Tutorial: http://efreedom.com/Question/1-31161...Time-Timestamp

    This is my version of the HTML:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Cattani Support Documents</title>
            <link href="/css/style.css" rel="stylesheet" type="text/css" media="screen" />
    
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
            <script type='text/javascript' src="/js/jquery.sort.js"></script>
    
        </head>
        <body>
    
    <div id="list">
    <div id="list_item_1" class="list_item">
      <div class="place">Beta</div>
      <div class="order">123456787</div>
      <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis quis ipsum rutrum metus rhoncus feugiat non vel orci. Etiam sit amet nisi sit amet est convallis viverra</div>
    </div>
    <div id="list_item_2" class="list_item">
      <div class="place">Alpha</div>
      <div class="order">123456788</div>
      <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis quis ipsum rutrum metus rhoncus feugiat non vel orci. Etiam sit amet nisi sit amet est convallis viverra</div>
    </div>
    <div id="list_item_3" class="list_item">
      <div class="place">Alpha</div>
      <div class="order">123456789</div>
      <div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis quis ipsum rutrum metus rhoncus feugiat non vel orci. Etiam sit amet nisi sit amet est convallis viverra</div>
    </div>
    </div>
    
    <script type="text/javascript">
    		//Sorts Documents by Position and Rating
    		$('#list .list_item').sortBy('.place', '.order');
    	</script>
    	
    	</body>
    </html>
    What I want to do is to first sort by div class="place" and then by div class="order". Where "place" is the section of the page and "order" is the ordering, from 1 - 10, within that section.

    I hope this makes it easier to understand.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •