Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Custom sort

  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom sort

    I already have a sort function that but wish to provide my users with the ability to specify custom sorts - days of the week, months etc.

    Assuming that I have a function that will return day of week (ie 'Mon' = 0, "Tue" = 1, "Wed" = 2 ... etc) can anyone show me how I would incorporate this into a sort routine?

    Many thanks in advance should you respond to this.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts
    I think the function you are looking for is in the Date() object
    See: http://www.w3schools.com/jsref/jsref_getDay.asp

    In this function, Sun=0, Mon=1, ...Sat=6
    but that can still be sorted on.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    Sort *what*???

    Sort an array?

    Or sort the visible screen contents?

    Sorting an array with a custom sort is easy:
    Code:
    var sorted = someArray.sort( nameOfSortFunction );
    And your custom sort function is called by Array.sort one pair of elements at a time, with a pair of arguments. Your function should return +1, 0, -1 to indicate whether the first element is larger, the same, or smaller than the second.

    Let's take a simple example:
    Code:
    <script type="text/javascript">
    
    function Holiday( when, name )
    {
        this.eventDate = when;
        this.eventName = name;
    }
    
    var data = [
        new Holiday( new Date( 2010, 9, 12 ), "Columbus Day" ),    
        new Holiday( new Date( 2010, 8, 6 ), "Labor Day" ),
        new Holiday( new Date( 2010, 0, 1 ), "New Years Day" )
        ];
    
    
    function compareHolidays( ev1, ev2 )
    {
        var dt1 = ev1.eventDate.getTime();
        var dt2 = ev2.eventDate.getTime();
        if ( dt1 < dt2 ) return -1;
        if ( dt1 > dt2 ) return 1;
        return 0;
    }
    
    var sorted = data.sort( compareHolidays );
    
    for ( var i = 0; i < sorted.length; ++i )
    {
       var ev = sorted[i];
       document.write( ev.eventDate.toString() + " :: " + ev.eventName + "<br/>" );
    }
    </script>
    See? Really pretty simple.

    Actually, the comparison function can be even simpler:
    Code:
    function compareHolidays( ev1, ev2 )
    {
        return ev1.eventDate.getTime() - ev2.eventDate.getTime();
    }
    Actually, instead of -1,0,+1 you can return <0,0,>0 with the same results. So it's pretty flexible.

    Naturally, to invert the sort you just reverse the comparison:
    Code:
    function compareHolidays( ev1, ev2 )
    {
        return ev2.eventDate.getTime() - ev1.eventDate.getTime();
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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