LucasSeveryn
LucasSeveryn

Reputation: 6262

Sorting array by numeric field in an object

I am trying to sort an array of objects by a field that holds numeric value. And this console output is the result of calling:

console.log(_.sortBy( d, 'userid' ));

[Object, Object, Object]
0: Object
count: "6"
name: "Andrey"
userid: "1234"
__proto__: Object
1: Object
count: "9"
name: "Lucas"
userid: "1337"
__proto__: Object
2: Object
count: "30"
name: "M"
userid: "7800"

And it looks fine, just as I expect it to happen.

However when I call this:

console.log(_.sortBy( d, 'count' ));

Instead of seeing array of order count 6, count 9, count 30, I see this:

Array[3]
0: Object
count: "30"
name: "M"
userid: "7800"
__proto__: Object
1: Object
count: "6"
name: "Andrey"
userid: "1234"
__proto__: Object
2: Object
count: "9"
name: "Lucas"
userid: "1337"

Can anyone help me understand what is happening and how can I fix it?

Upvotes: 0

Views: 93

Answers (1)

Jonathan
Jonathan

Reputation: 9151

Use parseInt(obj.count) before you sort.

Upvotes: 1

Related Questions