Sort array by year and month [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I have the array as below,
let ÃÂ yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
I want to sort the array by year first and after that sort month from the year,
I want the output like this,
yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "JANUARY " ,
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH"
]
How to achieve this?
javascript arrays angular sorting lodash
closed as too broad by jonrsharpe, greg-449, KooiInc, RiaD, 0x499602D2 Aug 18 at 19:21
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
7
down vote
favorite
I have the array as below,
let ÃÂ yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
I want to sort the array by year first and after that sort month from the year,
I want the output like this,
yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "JANUARY " ,
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH"
]
How to achieve this?
javascript arrays angular sorting lodash
closed as too broad by jonrsharpe, greg-449, KooiInc, RiaD, 0x499602D2 Aug 18 at 19:21
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Possible duplicate of Sort array by firstname (alphabetically) in Javascript
â RiaD
Aug 18 at 12:13
This question solution not an quite easy, but still many people posting this as duplicate with others..stupid things. finally accepted solution very fine working with me....Thank you everyone for help
â Aniket Avhad
Aug 18 at 12:19
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have the array as below,
let ÃÂ yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
I want to sort the array by year first and after that sort month from the year,
I want the output like this,
yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "JANUARY " ,
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH"
]
How to achieve this?
javascript arrays angular sorting lodash
I have the array as below,
let ÃÂ yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
I want to sort the array by year first and after that sort month from the year,
I want the output like this,
yearAndMonth ÃÂ = ÃÂ [
"year": 2013, "month": "JANUARY " ,
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH"
]
How to achieve this?
javascript arrays angular sorting lodash
edited Aug 18 at 9:38
Mohammad Usman
17k93251
17k93251
asked Aug 18 at 9:25
Aniket Avhad
1,188313
1,188313
closed as too broad by jonrsharpe, greg-449, KooiInc, RiaD, 0x499602D2 Aug 18 at 19:21
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by jonrsharpe, greg-449, KooiInc, RiaD, 0x499602D2 Aug 18 at 19:21
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Possible duplicate of Sort array by firstname (alphabetically) in Javascript
â RiaD
Aug 18 at 12:13
This question solution not an quite easy, but still many people posting this as duplicate with others..stupid things. finally accepted solution very fine working with me....Thank you everyone for help
â Aniket Avhad
Aug 18 at 12:19
add a comment |Â
Possible duplicate of Sort array by firstname (alphabetically) in Javascript
â RiaD
Aug 18 at 12:13
This question solution not an quite easy, but still many people posting this as duplicate with others..stupid things. finally accepted solution very fine working with me....Thank you everyone for help
â Aniket Avhad
Aug 18 at 12:19
Possible duplicate of Sort array by firstname (alphabetically) in Javascript
â RiaD
Aug 18 at 12:13
Possible duplicate of Sort array by firstname (alphabetically) in Javascript
â RiaD
Aug 18 at 12:13
This question solution not an quite easy, but still many people posting this as duplicate with others..stupid things. finally accepted solution very fine working with me....Thank you everyone for help
â Aniket Avhad
Aug 18 at 12:19
This question solution not an quite easy, but still many people posting this as duplicate with others..stupid things. finally accepted solution very fine working with me....Thank you everyone for help
â Aniket Avhad
Aug 18 at 12:19
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
0
down vote
accepted
You can also use lodash
library for sorting data by multiple column.
I have created a demo on Stackblitz. I hope this will help/guide to you/others.
lodash - Documentation
Component.html
<table width="100%">
<tr>
<td>Year</td>
<td>Month</td>
</tr>
<tr *ngFor="let datas of sortedData">
<td>datas.year</td>
<td>datas.month</td>
</tr>
</table>
Component.ts
sortedData: any;
data = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2013, "month": "MARCH" ,
"year": 2013, "month": "APRIL" ,
"year": 2015, "month": "FEBRUARY"
];
monthArray: any = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
ngOnInit()
this.sortedData = _.orderBy(data, [(datas) => datas.year, (user) => (this.monthArray.indexOf(user.month))], ["asc", "asc"]);
console.log(this.sortedData);
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
4
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,_.orderBy()
is also simple.
â Armfoot
Aug 18 at 18:00
add a comment |Â
up vote
3
down vote
You could take an object for the month names and their numerical value.
The chain the order by taking the delta of year and month.
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |Â
up vote
1
down vote
You can make a map which maps the Month to the month number and then use Arrays.sort()
with your own custom comparator :
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
add a comment |Â
up vote
1
down vote
You can create an array for months names and sort like this:
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |Â
up vote
1
down vote
Since you are ok with using lodash
this can be achived by a simple sortBy
_.sortBy(yearAndMonth, a => new Date(1+ a.month + a.year))
It will construct a new Date
for each month and year (with date 1) and that should work the way you want.
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Note: You do not need to have array/object/map of all the months for this to have a look up to perform >
or <
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
add a comment |Â
up vote
0
down vote
Declaring the month names in an array to get the relative value of the month string when comparing with each other.
First comparison will be on the year, if both the year values are same then proceeding with the month comparison based on the months array created.
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
yearAndMonth.sort((a,b) =>
if(a.year > b.year) return 1;
else if(a.year < b.year) return -1;
else
if(months.indexOf(a.month.toUpperCase()) >
months.indexOf(b.month.toUpperCase()))
return 1;
else if(months.indexOf(a.month.toUpperCase()) <
months.indexOf(b.month.toUpperCase()))
return -1
else return 0;
);
add a comment |Â
up vote
0
down vote
See also: JsFiddle
Allow me to provide a plain ES version (sort array of objects on 1 or more key values, serial dependant (sort on 1, sort 2 within 1, sort on 3 within 1 and 2 etc.), non mutating, i.e. keep the original array as is):
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can also use lodash
library for sorting data by multiple column.
I have created a demo on Stackblitz. I hope this will help/guide to you/others.
lodash - Documentation
Component.html
<table width="100%">
<tr>
<td>Year</td>
<td>Month</td>
</tr>
<tr *ngFor="let datas of sortedData">
<td>datas.year</td>
<td>datas.month</td>
</tr>
</table>
Component.ts
sortedData: any;
data = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2013, "month": "MARCH" ,
"year": 2013, "month": "APRIL" ,
"year": 2015, "month": "FEBRUARY"
];
monthArray: any = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
ngOnInit()
this.sortedData = _.orderBy(data, [(datas) => datas.year, (user) => (this.monthArray.indexOf(user.month))], ["asc", "asc"]);
console.log(this.sortedData);
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
4
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,_.orderBy()
is also simple.
â Armfoot
Aug 18 at 18:00
add a comment |Â
up vote
0
down vote
accepted
You can also use lodash
library for sorting data by multiple column.
I have created a demo on Stackblitz. I hope this will help/guide to you/others.
lodash - Documentation
Component.html
<table width="100%">
<tr>
<td>Year</td>
<td>Month</td>
</tr>
<tr *ngFor="let datas of sortedData">
<td>datas.year</td>
<td>datas.month</td>
</tr>
</table>
Component.ts
sortedData: any;
data = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2013, "month": "MARCH" ,
"year": 2013, "month": "APRIL" ,
"year": 2015, "month": "FEBRUARY"
];
monthArray: any = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
ngOnInit()
this.sortedData = _.orderBy(data, [(datas) => datas.year, (user) => (this.monthArray.indexOf(user.month))], ["asc", "asc"]);
console.log(this.sortedData);
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
4
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,_.orderBy()
is also simple.
â Armfoot
Aug 18 at 18:00
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can also use lodash
library for sorting data by multiple column.
I have created a demo on Stackblitz. I hope this will help/guide to you/others.
lodash - Documentation
Component.html
<table width="100%">
<tr>
<td>Year</td>
<td>Month</td>
</tr>
<tr *ngFor="let datas of sortedData">
<td>datas.year</td>
<td>datas.month</td>
</tr>
</table>
Component.ts
sortedData: any;
data = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2013, "month": "MARCH" ,
"year": 2013, "month": "APRIL" ,
"year": 2015, "month": "FEBRUARY"
];
monthArray: any = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
ngOnInit()
this.sortedData = _.orderBy(data, [(datas) => datas.year, (user) => (this.monthArray.indexOf(user.month))], ["asc", "asc"]);
console.log(this.sortedData);
You can also use lodash
library for sorting data by multiple column.
I have created a demo on Stackblitz. I hope this will help/guide to you/others.
lodash - Documentation
Component.html
<table width="100%">
<tr>
<td>Year</td>
<td>Month</td>
</tr>
<tr *ngFor="let datas of sortedData">
<td>datas.year</td>
<td>datas.month</td>
</tr>
</table>
Component.ts
sortedData: any;
data = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2013, "month": "MARCH" ,
"year": 2013, "month": "APRIL" ,
"year": 2015, "month": "FEBRUARY"
];
monthArray: any = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
ngOnInit()
this.sortedData = _.orderBy(data, [(datas) => datas.year, (user) => (this.monthArray.indexOf(user.month))], ["asc", "asc"]);
console.log(this.sortedData);
edited Aug 18 at 9:48
Aniket Avhad
1,188313
1,188313
answered Aug 18 at 9:28
Krishna Rathore
3,0181416
3,0181416
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
4
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,_.orderBy()
is also simple.
â Armfoot
Aug 18 at 18:00
add a comment |Â
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
4
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,_.orderBy()
is also simple.
â Armfoot
Aug 18 at 18:00
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
Thanks you for providing solution with lodash library.
â Aniket Avhad
Aug 18 at 9:51
4
4
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:
data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
Rather than importing an entire library to do a basic thing like sorting, try Vanilla JS:
data.sort(function(a,b) (monthArray.indexOf(a.month) - monthArray.indexOf(b.month));)
â Niet the Dark Absol
Aug 18 at 16:05
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,
_.orderBy()
is also simple.â Armfoot
Aug 18 at 18:00
@NiettheDarkAbsol I agree it's overkill, but if he is using the library already for other purposes,
_.orderBy()
is also simple.â Armfoot
Aug 18 at 18:00
add a comment |Â
up vote
3
down vote
You could take an object for the month names and their numerical value.
The chain the order by taking the delta of year and month.
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |Â
up vote
3
down vote
You could take an object for the month names and their numerical value.
The chain the order by taking the delta of year and month.
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You could take an object for the month names and their numerical value.
The chain the order by taking the delta of year and month.
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
You could take an object for the month names and their numerical value.
The chain the order by taking the delta of year and month.
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
var array = [ year: 2013, month: "FEBRUARY" , year: 2015, month: "MARCH" , year: 2013, month: "JANUARY" , year: 2015, month: "FEBRUARY" ];
array.sort(function (a, b)
var MONTH = JANUARY: 0, FEBRUARY: 1, MARCH: 2, APRIL: 3, MAY: 4, JUNE: 5, JULY: 6, AUGUST: 7, SEPTEMBER: 8, OCTOBER: 9, NOVEMBER: 10, DECEMBER: 11 ;
return a.year - b.year );
console.log(array);
.as-console-wrapper max-height: 100% !important; top: 0;
answered Aug 18 at 9:31
Nina Scholz
157k1277136
157k1277136
add a comment |Â
add a comment |Â
up vote
1
down vote
You can make a map which maps the Month to the month number and then use Arrays.sort()
with your own custom comparator :
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
add a comment |Â
up vote
1
down vote
You can make a map which maps the Month to the month number and then use Arrays.sort()
with your own custom comparator :
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can make a map which maps the Month to the month number and then use Arrays.sort()
with your own custom comparator :
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
You can make a map which maps the Month to the month number and then use Arrays.sort()
with your own custom comparator :
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
let months = 'JANUARY' : 1, 'FEBRUARY' : 2, 'MARCH' : 3, 'APRIL' : 4, 'MAY' : 5, 'JUNE' : 6, 'JULY' : 7, 'AUGUST' : 8, 'SEPTEMBER' : 9, 'OCTOBER' : 10, 'NOVEMBER' : 11, 'DECEMBER' : 12 ;
let yearAndMonth = [ "year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" , "year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY" ];
yearAndMonth.sort((a,b)=> a.year - b.year || months[a.month.toUpperCase()] - months[b.month.toUpperCase()]);
console.log(yearAndMonth);
answered Aug 18 at 9:32
amrender singh
4,2101518
4,2101518
add a comment |Â
add a comment |Â
up vote
1
down vote
You can create an array for months names and sort like this:
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |Â
up vote
1
down vote
You can create an array for months names and sort like this:
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can create an array for months names and sort like this:
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
You can create an array for months names and sort like this:
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
let data = [
"year": 2013, "month": "FEBRUARY" , "year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" , "year": 2015, "month": "FEBRUARY"
];
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
data.sort(
(a, b) => (a.year - b.year) || (months.indexOf(a.month) - months.indexOf(b.month))
);
console.log(data);
.as-console-wrapper max-height: 100% !important; top: 0;
answered Aug 18 at 9:36
Mohammad Usman
17k93251
17k93251
add a comment |Â
add a comment |Â
up vote
1
down vote
Since you are ok with using lodash
this can be achived by a simple sortBy
_.sortBy(yearAndMonth, a => new Date(1+ a.month + a.year))
It will construct a new Date
for each month and year (with date 1) and that should work the way you want.
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Note: You do not need to have array/object/map of all the months for this to have a look up to perform >
or <
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
add a comment |Â
up vote
1
down vote
Since you are ok with using lodash
this can be achived by a simple sortBy
_.sortBy(yearAndMonth, a => new Date(1+ a.month + a.year))
It will construct a new Date
for each month and year (with date 1) and that should work the way you want.
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Note: You do not need to have array/object/map of all the months for this to have a look up to perform >
or <
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Since you are ok with using lodash
this can be achived by a simple sortBy
_.sortBy(yearAndMonth, a => new Date(1+ a.month + a.year))
It will construct a new Date
for each month and year (with date 1) and that should work the way you want.
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Note: You do not need to have array/object/map of all the months for this to have a look up to perform >
or <
Since you are ok with using lodash
this can be achived by a simple sortBy
_.sortBy(yearAndMonth, a => new Date(1+ a.month + a.year))
It will construct a new Date
for each month and year (with date 1) and that should work the way you want.
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Note: You do not need to have array/object/map of all the months for this to have a look up to perform >
or <
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
let yearAndMonth = [
"year": 2013, "month": "FEBRUARY" ,
"year": 2015, "month": "MARCH" ,
"year": 2013, "month": "JANUARY" ,
"year": 2015, "month": "FEBRUARY"
]
let res = _.sortBy(yearAndMonth, a => new Date(1 + a.month + a.year));
console.log('Sorted Result: ', res);
.as-console-wrapper max-height: 100% !important; top: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
edited Aug 18 at 12:16
answered Aug 18 at 11:18
Koushik Chatterjee
2,2673923
2,2673923
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
add a comment |Â
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
I got the "Argument of type 'any' is not assignable to parameter of type 'string'" this error when i use your solution in angular.
â Aniket Avhad
Aug 18 at 12:09
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
@AniketAvhad either use [1, a.month, a.year].join('') or use simply with + operator without array, I have edited accordingly, you faced the problem of type checking at compile time.
â Koushik Chatterjee
Aug 18 at 12:17
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
Thanks, now your solution working fine.
â Aniket Avhad
Aug 18 at 12:23
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
I believe this should be promoted to the best answer as that will help the community, and the people visit to this question!
â Koushik Chatterjee
Aug 19 at 16:11
add a comment |Â
up vote
0
down vote
Declaring the month names in an array to get the relative value of the month string when comparing with each other.
First comparison will be on the year, if both the year values are same then proceeding with the month comparison based on the months array created.
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
yearAndMonth.sort((a,b) =>
if(a.year > b.year) return 1;
else if(a.year < b.year) return -1;
else
if(months.indexOf(a.month.toUpperCase()) >
months.indexOf(b.month.toUpperCase()))
return 1;
else if(months.indexOf(a.month.toUpperCase()) <
months.indexOf(b.month.toUpperCase()))
return -1
else return 0;
);
add a comment |Â
up vote
0
down vote
Declaring the month names in an array to get the relative value of the month string when comparing with each other.
First comparison will be on the year, if both the year values are same then proceeding with the month comparison based on the months array created.
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
yearAndMonth.sort((a,b) =>
if(a.year > b.year) return 1;
else if(a.year < b.year) return -1;
else
if(months.indexOf(a.month.toUpperCase()) >
months.indexOf(b.month.toUpperCase()))
return 1;
else if(months.indexOf(a.month.toUpperCase()) <
months.indexOf(b.month.toUpperCase()))
return -1
else return 0;
);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Declaring the month names in an array to get the relative value of the month string when comparing with each other.
First comparison will be on the year, if both the year values are same then proceeding with the month comparison based on the months array created.
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
yearAndMonth.sort((a,b) =>
if(a.year > b.year) return 1;
else if(a.year < b.year) return -1;
else
if(months.indexOf(a.month.toUpperCase()) >
months.indexOf(b.month.toUpperCase()))
return 1;
else if(months.indexOf(a.month.toUpperCase()) <
months.indexOf(b.month.toUpperCase()))
return -1
else return 0;
);
Declaring the month names in an array to get the relative value of the month string when comparing with each other.
First comparison will be on the year, if both the year values are same then proceeding with the month comparison based on the months array created.
let months = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
yearAndMonth.sort((a,b) =>
if(a.year > b.year) return 1;
else if(a.year < b.year) return -1;
else
if(months.indexOf(a.month.toUpperCase()) >
months.indexOf(b.month.toUpperCase()))
return 1;
else if(months.indexOf(a.month.toUpperCase()) <
months.indexOf(b.month.toUpperCase()))
return -1
else return 0;
);
answered Aug 18 at 9:51
Amardeep Bhowmick
6771416
6771416
add a comment |Â
add a comment |Â
up vote
0
down vote
See also: JsFiddle
Allow me to provide a plain ES version (sort array of objects on 1 or more key values, serial dependant (sort on 1, sort 2 within 1, sort on 3 within 1 and 2 etc.), non mutating, i.e. keep the original array as is):
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
add a comment |Â
up vote
0
down vote
See also: JsFiddle
Allow me to provide a plain ES version (sort array of objects on 1 or more key values, serial dependant (sort on 1, sort 2 within 1, sort on 3 within 1 and 2 etc.), non mutating, i.e. keep the original array as is):
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
add a comment |Â
up vote
0
down vote
up vote
0
down vote
See also: JsFiddle
Allow me to provide a plain ES version (sort array of objects on 1 or more key values, serial dependant (sort on 1, sort 2 within 1, sort on 3 within 1 and 2 etc.), non mutating, i.e. keep the original array as is):
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
See also: JsFiddle
Allow me to provide a plain ES version (sort array of objects on 1 or more key values, serial dependant (sort on 1, sort 2 within 1, sort on 3 within 1 and 2 etc.), non mutating, i.e. keep the original array as is):
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
const log = (...str) =>
document.querySelector("pre").textContent += `$str.join("n")n`;
const data = getData();
const xSort = XSort();
const months = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
log( JSON.stringify(
xSort
.create(data)
.orderBy( key: "year", key: v => months.indexOf(v.month) ),
null,
" ")
);
function XSort()
const multiSorter = sortKeys =>
const Sorter = function (array)
this.array = array;
;
Sorter.prototype =
orderBy: function(...sortOns)
return this.array.slice().sort(multiSorter(sortOns));
,
;
return
create: array => new Sorter(array)
;
function getData()
return [
"year": 2013,
"month": "FEBRUARY",
,
"year": 2015,
"month": "MARCH",
,
"year": 2015,
"month": "SEPTEMBER",
,
"year": 2013,
"month": "JANUARY",
,
"year": 2013,
"month": "MARCH",
,
"year": 2013,
"month": "APRIL",
,
"year": 2015,
"month": "FEBRUARY",
];
<pre></pre>
edited Aug 19 at 7:40
answered Aug 18 at 16:43
KooiInc
77.6k20105137
77.6k20105137
add a comment |Â
add a comment |Â
Possible duplicate of Sort array by firstname (alphabetically) in Javascript
â RiaD
Aug 18 at 12:13
This question solution not an quite easy, but still many people posting this as duplicate with others..stupid things. finally accepted solution very fine working with me....Thank you everyone for help
â Aniket Avhad
Aug 18 at 12:19