I seem to be having a little issue getting an array of objects to sort properly.
For example, let's say I have the following:
var items = [
{id: '1', itn: '1'},
{id: '2', itn: '10P'},
{id: '3', itn: '2'},
{id: '4', itn: '3'}
];
I need to sort this array based on the value in "itn." However, when I run the function below I get it listed as 1, 10P, 2, 3 instead of 1, 2, 3, 10P.
userData.sort(function (a, b) {
return (b.itn > a.itn) ? -1 : (b.itn < a.itn) ? 1 : 0;
});
I'm a bit stuck in how to go forward with this and am also unsure what kind of keywords to search for to get the correct guidance. Any help is appreciated!
Thanks in advance.
<are strings, it compares lexicographically;"10P"is before"2"just like"aardvark"is before"b".parseIntif you want numeric comparison.