Skip to content

Instantly share code, notes, and snippets.

@onpubcom
Created February 8, 2012 20:15
Show Gist options
  • Save onpubcom/1772996 to your computer and use it in GitHub Desktop.
Save onpubcom/1772996 to your computer and use it in GitHub Desktop.

Revisions

  1. onpubcom revised this gist Feb 8, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion onpubcom_array_date_sort.js
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,6 @@ for (var i = 0; i < dates.length; i++) {
    }

    // That's all there is to it!
    // By Onpub.com. http://onpub.com/index.php?s=7&a=109
    // From: http://onpub.com/index.php?s=7&a=109

    </script>
  2. onpubcom revised this gist Feb 8, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion onpubcom_array_date_sort.js
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,6 @@ for (var i = 0; i < dates.length; i++) {
    }

    // That's all there is to it!
    // By Onpub.com. More info: http://onpub.com/index.php?s=7&a=109
    // By Onpub.com. http://onpub.com/index.php?s=7&a=109

    </script>
  3. onpubcom revised this gist Feb 8, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions onpubcom_array_date_sort.js
    Original file line number Diff line number Diff line change
    @@ -51,5 +51,6 @@ for (var i = 0; i < dates.length; i++) {
    }

    // That's all there is to it!
    // By Onpub.com. More info: http://onpub.com/index.php?s=7&a=109

    </script>
  4. onpubcom created this gist Feb 8, 2012.
    55 changes: 55 additions & 0 deletions onpubcom_array_date_sort.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    <script type="text/javascript">

    // First let's create an array of JavaScript Date
    // objects.
    // More info about the Date class:
    // http://w3schools.com/js/js_obj_date.asp
    var dates = [
    new Date(2010, 4, 10, 10, 07, 16),
    new Date(2010, 4, 8, 9, 16, 09),
    new Date(2010, 3, 30, 0, 15, 49),
    new Date(2010, 3, 8, 10, 08, 35)];

    // Now we will define our date comparison functions. These are callbacks
    // that we will be providing to the array sort method below.
    var date_sort_asc = function (date1, date2) {
    // This is a comparison function that will result in dates being sorted in
    // ASCENDING order. As you can see, JavaScript's native comparison operators
    // can be used to compare dates. This was news to me.
    if (date1 > date2) return 1;
    if (date1 < date2) return -1;
    return 0;
    };

    var date_sort_desc = function (date1, date2) {
    // This is a comparison function that will result in dates being sorted in
    // DESCENDING order.
    if (date1 > date2) return -1;
    if (date1 < date2) return 1;
    return 0;
    };

    // Finally, we are now able to call the sort method on our array of dates.
    // More info about array sorting: http://w3schools.com/jsref/jsref_sort.asp

    // First let's sort the array in ascending order.
    dates.sort(date_sort_asc);

    // Now let's output the results to the page to show that the dates are now
    // sorted in ascending order.
    document.write('<p>Dates sorted in ascending order (oldest to newest):</p>');
    for (var i = 0; i < dates.length; i++) {
    document.write(i + ': ' + dates[i] + '<br>');
    }

    // Now let's sort the dates in descending order and output the results.
    dates.sort(date_sort_desc);

    document.write('<p>Dates sorted in descending order (newest to oldest):</p>');
    for (var i = 0; i < dates.length; i++) {
    document.write(i + ': ' + dates[i] + '<br>');
    }

    // That's all there is to it!

    </script>