How to sort a dynamic array

I have a dynamic array created as follows:

window.IDarray = []; 

And I have a dictionary created like this:

 window.itemdictionary = {}; 

The window.IDarray same as the window.itemdictionary . And the window.IDarray values window.IDarray unique. Also, window.IDarray values ​​are keys to window.itemdictionary .

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

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

+4
source share
3 answers

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

First, to sort by date, your "modified": "Mon May 28 11:20:46 EDT 2012" needs to be converted to a format that can be used for comparison 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 IDarray through .sort() using a custom sort function:

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

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

+3
source

Sorting an array using a special parameter of the comparator function, for example:

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

0
source

Source: https://habr.com/ru/post/1483437/


All Articles