Reputation: 766
I have an array as follows :
var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];
In this array shows me the month and year , I want to sort by month and year as follows :
var monthYear = ["7-2015", "8-2015", "9-2015", "10-2015", "11-2015", "12-2015", "1-2016", "2-2016", "3-2016", "4-2016", "5-2016", "6-2016", "7-2016", "8-2016"];
Similarly with the weeks
var weekYear = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-44", "2015-49", "2016-06", "2016-09", "2015-43", "2015-45", "2015-46", "2015-47", "2015-48", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-07", "2016-08", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];
to order it follows
var weekYear = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-43", "2015-44", "2015-45", "2015-46", "2015-47", "2015-48", "2015-49", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];
I tried using this function but it does not work :
monthYear = monthYear.sort(sortNumber);
function sortNumber(elem1, elem2){
var as = elem1.split('-');
var bs = elem2.split('-');
if(parseInt(as[0])< parseInt(bs[0])){
if(parseInt(as[1]) < parseInt(bs[1])){
return -1;
}else if(parseInt(as[1]) > parseInt(bs[1])){
return 1;
}else{
return 0;
}
}else if(parseInt(as[0]) > parseInt(bs[0])){
return 1;
}
return 0;
}
Similarly with the weeks , I wonder if the function is wrong or if there is an easier way to do it .
Upvotes: 2
Views: 9512
Reputation: 1390
You could just turn each date in the array into an actual date within the custom sort function and sort it by actual date:
var monthYear = ["June 2017","October 2018","September 2017"];
monthYear.sort(sortByMonthYear);
function sortByMonthYear(a, b) {
var as = a.split(' '),
bs = b.split(' '),
ad = new Date(as[0] + ' 1,' + as[1]),
bd = new Date(bs[0] + ' 1,' + bs[1]);
return ad.getTime() - bd.getTime();
}
Please have a look : Click here
Upvotes: 1
Reputation: 296
here is working code, for monthYear:
var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016",
"6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015",
"11-2015", "12-2015"];
function sortMonthYear(myarray){
let temp;
let tempArr=new Array(myarray.length);
//make sort friendly
for (i=0; i<myarray.length; i++){
temp = myarray[i].split('-');
if(temp[0]<10){
temp[0]= "0"+temp[0];
}
tempArr[i] =temp[1]+'-'+temp[0];
}
//use sort
tempArr.sort();
//back to previous format
for (i=0; i<tempArr.length; i++){
temp = tempArr[i].split('-');
if (temp[1].charAt(0)==0){
temp[1]= temp[1].substring(1);
}
tempArr[i] = temp[1]+'-'+temp[0];
}
return tempArr;
}
var monthYear = sortMonthYear(monthYear);
console.log(monthYear)
for the weekYear a .sort() is enough
Upvotes: 0
Reputation: 73241
You can create a Date
out of your strings and sort them acccordingly:
var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];
var sorted = monthYear.sort(function(a,b) {
a = a.split("-");
b = b.split("-")
return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
});
console.log(sorted);
// --> ["7-2015", "8-2015", "9-2015", "10-2015", "11-2015", "12-2015", "1-2016", "2-2016", "3-2016", "4-2016", "5-2016", "6-2016", "7-2016", "8-2016"]
And to sort the week:
Please refer to @mplungjan's answer
Upvotes: 9
Reputation: 178026
Just concatenate with padding and return the difference
function pad(num) {
return String("0" + num).slice(-2);
}
function sortXXYear(elem1, elem2) {
var as = elem1.split('-'),
bs = elem2.split('-'),
aa = as[1]+pad(as[0]), // yyyymm
bb = bs[1]+pad(bs[0]);
return aa - bb;
}
var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];
monthYear.sort(sortXXYear);
console.log(monthYear);
No need to use a function to sort the week array
var yearWeek = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-44", "2015-49", "2016-06", "2016-09", "2015-43", "2015-45", "2015-46", "2015-47", "2015-48", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-07", "2016-08", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];
yearWeek.sort(); // No need to use special sort for YYYY-WW
console.log(yearWeek);
Upvotes: 2
Reputation: 4806
const mysort = (a,b) => {
const [ma,ya] = a.split('-').map(x=>Number(x))
const [mb,yb] = b.split('-').map(x=>Number(x))
return ya < yb ? -1 : ya > yb ? 1 : ma < mb ? -1 : ma > mb ? 1 : 0
}
const ywsort = (a,b) => {
const [ya,wa] = a.split('-').map(x=>Number(x))
const [yb,wb] = b.split('-').map(x=>Number(x))
return ya < yb ? -1 : ya > yb ? 1 : wa < wb ? -1 : wa > wb ? 1 : 0
}
monthYear.sort(mysort)
weekYear.sort(ywsort)
Upvotes: 1
Reputation: 386654
You could split the string and sort by priority without using Date
.
var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"],
weekYear = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-44", "2015-49", "2016-06", "2016-09", "2015-43", "2015-45", "2015-46", "2015-47", "2015-48", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-07", "2016-08", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];
monthYear.sort(function sort(a, b) {
var aa = a.split('-'),
bb = b.split('-');
return aa[1] - bb[1] || aa[0] - bb[0];
});
weekYear.sort(function sort(a, b) {
var aa = a.split('-'),
bb = b.split('-');
return aa[0] - bb[0] || aa[1] - bb[1];
});
console.log(monthYear);
console.log(weekYear);
If you have always the pattern 9999-99
for weekYear
, you could sort just with the built-in sort.
var weekYear = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-44", "2015-49", "2016-06", "2016-09", "2015-43", "2015-45", "2015-46", "2015-47", "2015-48", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-07", "2016-08", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];
weekYear.sort();
console.log(weekYear);
Upvotes: 2