I would like to know how to shift based on field that contains numeric string and normal string.
so, if numeric string value then shift to end of array, rest remain same
Tried
const sortData = arrobj.sort((a: any, b: any) => {
const sortA = a.cn;
const sortB = b.cn;
if(!isNaN(sortA)) {
return -1;
}
if(!isNaN(sortB)) {
return 1;
}
return sortA.localeCompare(sortB);
});
console.log("sortData", sortData);
var arrobj=[
{cn: '99'}
{cn: 'SG'},
{cn: 'IN'}
]
expecteded output
[
{cn: 'SG'},
{cn: 'IN'},
{cn: '99'}
]
2
Answers
Hmm… I think that you just need to add extra conditions to your ifs…
The OP had many syntactical errors, get in the habit of using a tool like JSHint. The order you want is just in reverse so use
.reverse()
Array method on the array after it’s sorted or just switch the parameters of.localeCompre()
. Also,.localeCompare()
doesn’t handle numbers correctly, so I updated to reflect that. Details are commented in example.Using
.reverse()