This is my data ["2","1","10"]
. When I am trying to sort these numbers my output is ["1","10","2"]
, but my expected output is ["1","2","10]
. I am using orderBy to sort these numbers. It sorts depending upon 0 to 9 only.
How can I sort these numbers using js?
Here is my code:
ng-options="option.id as option.roomno for option in frData
| unique:'roomno' | orderBy:'roomno' "
function sortNumStr(m,n) {
return m - n;
}
var numArray = ["2", "1", "10"];
numArray.sort(sortNumStr);
alert(numArray)
You can use the string's native prototype localeCompare
function like so:
["2","1","10"].sort((a,b) => a.localeCompare(b, undefined, { numeric: true }))
It works well with numbers among other chars too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.