Sorting by text, then numbers

Hi,

I have this code working, it’s sorting by text, then a timestamp:

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

$(".list_item").sortBy('.count', '.timestamp');

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

Thank you! :slight_smile:

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.

Hi Raffles,

I got the jQuery from a Tutorial: http://efreedom.com/Question/1-3116172/JQuery-Sorting-Div-Elements-Based-Count-Date-Time-Timestamp

This is my version of the HTML:


<!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. :slight_smile:

Got it working :slight_smile: