如何对动态数组进行排序

时间:2022-04-28 18:01:38

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.

window.IDarray的长度与window.itemdictionary相同。 window.IDarray的值是唯一的。 window.IDarray的值也是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".

window.itemdictionary中任何键的“value”的数据类型也是包含名为“modified”的键的字典,值是格式示例“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).

对window.IDarray的值进行排序的最佳方法是什么,以便从index 0到window.IDarray的末尾,它在window.itemdictionary中的相应日期离当前日期越来越远了? (即索引0将给出与当前日期最接近的日期,索引n将给出最远的日期)。

3 个解决方案

#1


3  

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

您需要使用自定义排序函数,请参阅MDN中的Array.sort。

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().

首先,为了按日期排序,需要使用Date.parse()将“修改后的”:“Mon May 28 11:20:46 EDT 2012”转换为可用于比较的格式。

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:

然后我们使用自定义排序函数通过.sort()运行IDarray:

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

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

见工作示例:http://jsfiddle.net/788bs/1/

#2


1  

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? */
    }
});

#3


0  

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/

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

#1


3  

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

您需要使用自定义排序函数,请参阅MDN中的Array.sort。

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().

首先,为了按日期排序,需要使用Date.parse()将“修改后的”:“Mon May 28 11:20:46 EDT 2012”转换为可用于比较的格式。

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:

然后我们使用自定义排序函数通过.sort()运行IDarray:

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

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

见工作示例:http://jsfiddle.net/788bs/1/

#2


1  

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? */
    }
});

#3


0  

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/

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