SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I'm completely stumped on the sort on page 298

    I am deleting this entire original question because I accidentally hit the post instead of the save button that is so plain I missed it, and so all my edit was lost!

    I figured out the one question but I am thinking the code below would get into an endless loop if two items to sort were equal because it never returns a 0 to alert the javascript sort function that the two items are equal... Is this correct?

    The code ...

    $(document).ready(function() {
    $('#ascending').click(function() {
    SORTER.sort('.sortable');
    });
    $('#descending').click(function() {
    SORTER.sort('.sortable', 'desc');
    });
    });

    var SORTER = {};
    SORTER.sort = function(which, dir) {
    SORTER.dir = (dir == "desc") ? -1 : 1;
    $(which).each(function() {
    // Find the list items and sort them
    var sorted = $(this).find("> li").sort(function(a, b) {
    return $(a).text().toLowerCase() > $(b).text().toLowerCase() ? SORTER.dir : -SORTER.dir;
    });
    $(this).append(sorted);
    });
    };
    Last edited by GarryFre; Jun 12, 2010 at 23:06. Reason: I figured it out, but I have one question..

  2. #2
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After fighting to correct all the errors in my post that hide till I hit submit, I forgot to mention that this code is from page 298 of the Novice To Ninja book from SitePoint.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    This line here is where the comparison occurs.

    Code javascript:
    return $(a).text().toLowerCase() > $(b).text().toLowerCase() ? SORTER.dir : -SORTER.dir;

    It returns one value when it's greater, if it's not (therefore being less than or equal to) returns the other value.

    There doesn't appear to be any conflict.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    This line here is where the comparison occurs.

    Code javascript:
    return $(a).text().toLowerCase() > $(b).text().toLowerCase() ? SORTER.dir : -SORTER.dir;

    It returns one value when it's greater, if it's not (therefore being less than or equal to) returns the other value.

    There doesn't appear to be any conflict.
    There's no conflict, its just that there are three possible values to return when sorting according to page 299, those are 1 to indicate that a is > b, -1 to indicate that a < b, and a third value = to indicate that a equals b. The third value of 0 is not needed here.

    I just did testing of this to see if two items of equal value caused a problem. It did not, I guess sort is smarter than that.

    t.
    Last edited by GarryFre; Jun 13, 2010 at 08:18. Reason: Tested this and found its not a problem.


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
  •