Question

I've been trying to solve this for a while with no success. I have 2 arrays:

var dates = [
    new Date(2014, 4, 11, 10, 10, 0).getTime(),
    new Date(2012, 4, 10, 8, 20, 0).getTime(),
    new Date(2013, 4, 9, 7, 20, 0).getTime(),
    new Date(2010, 4, 7, 12, 59, 0).getTime()
            ];

var newsItems = ["NewsItem1","NewsItem2","NewsItem3","NewsItem4"];

I'm using the function var desc = dates.sort(function(a,b){return b-a}); To sort the date array, however, I haven't been able to figure out how to also rearrange the newsItems array to align it with the dates array. I tried this but it doesn't seem to work:

var datesDuplicate = [
    new Date(2014, 4, 11, 10, 10, 0).getTime(),
    new Date(2012, 4, 10, 8, 20, 0).getTime(),
    new Date(2013, 4, 9, 7, 20, 0).getTime(),
    new Date(2010, 4, 7, 12, 59, 0).getTime()
            ];
for(var i = 0; i < arrayLength; i++){
    for(var j = 0; j < arrayLength; j++){
        if(dates[i] == datesDuplicate[j]){
            document.getElementById(getElement(i)).innerHTML = newsItems[j];
            break;
        }
    }

}

Any help is highly appreciated.

Was it helpful?

Solution

If you want the news items and dates to be associated to each other they should be part of the same object.

 var items = [
   { date: new Date(2014, 4, 11, 10, 10, 0).getTime(), label: "NewsItem1" },
   { date: new Date(2012, 4, 10, 8, 20, 0).getTime(), label: "NewsItem2" },
   etc...
 ];

and then you can sort it like

 var desc = items.sort(function(a,b){return b.date-a.date});

OTHER TIPS

try something like this

var news = [
   {date: new Date(2014, 4, 11, 10, 10, 0).getTime(), item: "NewsItem1" },
   {date: new Date(2012, 4, 10, 8, 20, 0).getTime(), item: "..." },
   {date: new Date(2013, 4, 9, 7, 20, 0).getTime(), item: "..." },
   {...}
];

You'll need to tweak your sort function, too. If you have to convert from two arrays, try combining them into the above structure with a zip.

Your abstraction could be improved by pairing dates with newsItems:

var datePairs = 
[
  ["NewsItem1", new Date(2014, 4, 11, 10, 10, 0).getTime()],
  ["NewsItem2", new Date(2012, 4, 10, 8, 20, 0).getTime()],
  ["NewsItem3", new Date(2013, 4, 9, 7, 20, 0).getTime()],
];

var desc = datePairs.sort(function(a,b){return b[1]-a[1]}); //sort by date (the second element of each element)

If you didn't have any control over how the data is structured and were stuck with the 2 arrays (which were dense and had the same length), then using ECMA5 methods you could sort them like this.

Javascript

var dates = [
        new Date(2014, 4, 11, 10, 10, 0).getTime(),
        new Date(2012, 4, 10, 8, 20, 0).getTime(),
        new Date(2013, 4, 9, 7, 20, 0).getTime(),
        new Date(2010, 4, 7, 12, 59, 0).getTime()
    ],
    newsItems = ["NewsItem1", "NewsItem2", "NewsItem3", "NewsItem4"];

dates.map((function (date, index) {
    return {
        date: date,
        item: this[index]
    };
}), newsItems).sort(function (a, b) {
    return b.date - a.date;
}).forEach(function (record, index) {
    dates[index] = record.date;
    newsItems[index] = record.item
});

console.log(dates, newsItems);

Output

[1399795800000, 1368076800000, 1336630800000, 1273229940000] ["NewsItem1", "NewsItem3", "NewsItem2", "NewsItem4"] 

On jsFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top