I have to sort JavaScript array with more than one filed combination and in that one field is combination of numbers & string.
My Code Snippet:
emps = [
{'first_name':'PQR','grade':'K'},
{'first_name':'ABC','grade':'K'},
{'first_name':'LMN','grade':'4'},
{'first_name':'CDE','grade':'2'},
{'first_name':'JKP','grade':'12'},
{'first_name':'ASD','grade':'Others'}
];
I am able to sort above array using following code:
emps.sort((a, b) => {
if (a.grade === "K" || b.grade === "Other") {
return -1;
}
if (a.grade === "Other" || b.grade === "K") {
return 1;
}
return +a.grade - +b.grade;
});
It's sorting gradewise accurately (K,1,2,3.....,12,Other) but I also want to sort it as per first_name with grade.
will you please suggest me the changes or script.