omega
omega

Reputation: 43843

How to sort a dynamic array

I have a dynamic array created like this:

window.IDarray = [];

And I have a dictionary created like this:

window.itemdictionary = {};

The length of window.IDarray is the same as window.itemdictionary. And the values of window.IDarray are unique. Also the values of window.IDarray are the keys to the window.itemdictionary.

The datatype of the "value" of any key in window.itemdictionary is also a dictionary that contains a key called "modified" and the value is a string date of the format example "Mon May 28 11:20:46 EDT 2012".

What is the best way to sort the values of window.IDarray, so that going from index 0 to the end of window.IDarray, its corresponding dates in window.itemdictionary are getting farther from the current date? (i.e. index 0 will give closest date to the current date, and index n will give the date farthest away).

Upvotes: 4

Views: 874

Answers (3)

Nebojša Stojković
Nebojša Stojković

Reputation: 1

window.IDarray = [];
window.itemdictionary = {
    "key0": { modified: "Mon May 28 11:20:46 EDT 2012" },
    "key1": { modified: "Mon May 28 11:20:46 EDT 2012" },
    "key2": { modified: "Mon Sep 20 20:35:15 EDT 2010" },
    "key3": { modified: "Mon May 10 10:07:16 EDT 2010" },
    "key4": { modified: "Tue May 10 10:07:16 EDT 2011" }
};

var sortByDate = function(key1, key2) {
    var date1 = new Date(window.itemdictionary[key1].modified.toString());
    var date2 = new Date(window.itemdictionary[key2].modified.toString());
    return date2 - date1;
};
// lt IE9
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt /*, from*/) {
        var len = this.length >>> 0;
        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0)
          from += len;

        for (; from < len; from++) {
          if (from in this && this[from] === elt)
            return from;
        }
        return -1;
    };
}

window.itemdictionary.currDate = { modified: new Date().toString() };
window.IDarray = Object.keys(window.itemdictionary);
console.log('before', window.IDarray);
window.IDarray.sort(sortByDate);

delete window.itemdictionary.currDate;
window.IDarray.splice(window.IDarray.indexOf('currDate'), 1);
console.log('after', window.IDarray);

http://jsfiddle.net/nYWmZ/1/

Upvotes: 0

jxpx777
jxpx777

Reputation: 3641

Sort the array with a custom comparator function parameter like:

IDarray.sort(function(a, b) {
    var date_a, date_b;
    try {
        date_a = Date.parse(itemdictionary[a]['modified'];
        date_b = Date.parse(itemdictionary[b]['modified'];
        return date_a - date_b;
    } catch (e) {
        /* Some smart exception handling for malformed strings? */
    }
});

Upvotes: 1

Amy
Amy

Reputation: 7466

You will need to use a custom sort function, see Array.sort from MDN.

First, in order to sort by a date, your "modified": "Mon May 28 11:20:46 EDT 2012" needs to be converted to a format that can be used for comparisons, using Date.parse().

var tempItemDictionary = [];   // use temp array to hold the timestamp
// convert dates first
for (var i = 0, item = null; i < IDarray.length; i++) {
    item = itemDictionary[IDarray[i]];
    tempItemDictionary[IDarray[i]] = {
        timestamp: Date.parse(item.modified)    // convert date to timestamp
    };
}

Then we run the IDarray through .sort() using the custom sorting function:

IDarray.sort(function(a, b) {
    return tempItemDictionary[b].timestamp - tempItemDictionary[a].timestamp;
});

See working example: http://jsfiddle.net/788bs/1/

Upvotes: 3

Related Questions