Date Sort

By vaseline28 on Nov 11, 2009

    This script sorts a list of dates into order - old -> new
    Can be used for dates which do not exist yet!

    To use this simply use the code:

        ago(VAR1, new Array(DATE1), new Array(DATE2));

    Adding as many dates within arrays and seperated by commas as you like.

        VAR1 = the date which you want the script to return:
            e.g. 0 = oldest date
                   1 = second oldest date
                   2 = third oldest date
                   etc.

        DATE1 = must be in the following format within the array:

            new Array(1, 2, 3, 4, 5)

            1 = minutes of the hour
            2 = hours of the day
            3 = date of the month
            4 = month of the year
            5 = year

        You can have as many dates as you like.

    Example of code use:

        alert(ago(1, new Array(1, 12, 29, 0, 2004), new Array(13,4,29,0,2005), new Array(24, 15, 13, 11, 9011), new Array(25, 11, 16, 10, 1456)));
function ago()
{
    var x = 0; date = new Array(); y = 0; returns = new Array();

    while (x < arguments.length)
    {
        if (typeof(arguments[x]) != "number")
        {

            var curArgument = arguments[x];

            date[x] = new Date();
                    date[x].setMinutes(curArgument[0]);
                    date[x].setHours(curArgument[1]);
                    date[x].setDate(curArgument[2]);
                    date[x].setMonth(curArgument[3]);
                    date[x].setFullYear(curArgument[4]);

        }

        x++;
    }

    date.valueOf().sort(function(a,b)
    {
        return a - b;
    });

    return (typeof(arguments[0]) == "number") ? date[arguments[0]] : date;
}

Comments

Sign in to comment.
Korvin   -  Mar 02, 2010

looks good.

ugh java, i cant really judgejavascript.

 Respond  
vaseline28   -  Nov 12, 2009

I would usually either cut out the time section and use it to work out who's older or I would use it to order a group of events, e.g.:

User1 Logged In
User2 Posted A Comment

As a rough example. To be honest, I was really just making it for fun and to experiment with not limited variables passed to the function.

 Respond  
Hawkee   -  Nov 11, 2009

Seems simple enough, what do you use it for?

 Respond  
Ghost-writer   -  Nov 11, 2009

ugh java, i cant really judge.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.