Reputation: 398
I have an array look like:
var v = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"]
What I want to do is sort this dynamically into a new empty array nv
. When the sorting is done nv
should look like.
var nv = [["07/27/2015", "07/28/2015"], ["08/29/2015", "08/29/2015"], ["07/27/2016"]]
Is it possible to sort like this way?
Upvotes: 1
Views: 1161
Reputation: 9825
So I made a function that puts the dates into an object whose properties are month and year. A date is put into the property of its month and year. The function then creates an array and creates an inner array for every property of the function. In each inner array it puts all the dates of that property. I figured this approach would be more efficient than nested for loops.
// function takes an array of dates in the following format MM/DD/YYYY
// outputs an array with inner arrays of dates. Each inner array contains dates of the same month and year
var groupDates = function(dateArray) {
// create object to organize dates by month and year
var dateHash = {};
// the array that is outputted
var groupedDates = [];
//for every date in dateArray
dateArray.forEach(function(currentDate) {
// check if any other dates with the same month and year exist in the dateHash object
if (dateHash[currentDate.substr(0, 2) + currentDate.substr(6)]) {
// if other dates exist, push the date to the array in the dateHash property for the dates current month and year
dateHash[currentDate.substr(0, 2) + currentDate.substr(6)].push(currentDate);
} else {
// otherwise create a property for the dates month and year and store the current date in an array in the propery
dateHash[currentDate.substr(0, 2) + currentDate.substr(6)] = [currentDate];
}
});
// for every propery in the datehash, push the array of dates into the grouped dates array
for (var dateGroup in dateHash) {
groupedDates.push(dateHash[dateGroup]);
}
return groupedDates;
};
var dateArray = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"];
console.log(groupDates(dateArray));
Upvotes: 3
Reputation: 2871
You can loop over the array and check for each value if it has a new month and year, or it's already included in the sorted array. I think like this untested code:
new_arr = new Array();
for(var i=0; i < v.length; i++){
var this_date = new Date(v[i]);
var month_and_year = this_date.getMonth() + this_date.getFullYear();
if(typeof(new_arr[month_and_year]) == 'undefined'){
new_arr[month_and_year] = new Array();
}
new_arr[month_and_year].push(v[i])
}
Upvotes: 0
Reputation: 21881
var dates = ["07/27/2015", "07/28/2015", "08/29/2015", "08/29/2015", "07/27/2016"];
var groupedDates = dates.reduce(function(l, r) {
var keyParts = r.split("/"),
key = keyParts[2] + keyParts[0];
if (typeof l[key] === "undefined") {
l[key] = [];
}
l[key].push(r);
return l;
}, {});
var result = Object.keys(groupedDates)
.sort(function(a, b) { return Number(a) - Number(b); })
.map(function(key) {
return groupedDates[key];
});
console.log(result); // [["07/27/2015","07/28/2015"],["08/29/2015","08/29/2015"],["07/27/2016"]]
Upvotes: 4