0

I have below array of objects , and i want to sort it .

     var points = [
{
    val: '0.10',
    text: '$0.10'
},
{
    val: '0.50',
    text: '$0.50'
},
{
    val: '1',
    text: '$1'
},
{
    val: '2',
    text: '$2'
},
{
    val: '3',
    text: '$3'
},
{
    val: '10',
    text: '$10'
},
{
    val: '20',
    text: '$20'
},
{
    val: '25',
    text: '$25'
},
{
    val: '50',
    text: '$50'
},
{
    val: '75',
    text: '$75'
},
{
    val: '100',
    text: '$100'
},
{
    val: '150',
    text: '$150'
},
{
    val: '200',
    text: '$200'
},
{
    val: '250',
    text: '$250'
},
{
    val: '300',
    text: '$300'
},
{
    val: '400',
    text: '$400'
},
{
    val: '1000',
    text: '$1,000'
},
{
    val: 'Other',
    text: 'Other'
}

]

I have tried below sorting function -

        function myFunction() {
          points.sort(function(a, b){return a.val - b.val});
          console.log(points)
        }

But this is returning me wrong output. I have also tried using orderby and sortby but still getting wrong output.

_.orderBy(points,['val'],['asc'])

_.sortBy(points, 'val')

By using both options i am getting wrong sorted values.

Community
  • 1
  • 1
Kalashir
  • 1,099
  • 4
  • 15
  • 38

1 Answers1

0

Use this sort method -

points.sort(function(a,b){
 return parseInt(a.val)  - parseInt(b.val);
})
Ayush Sharma
  • 2,057
  • 15
  • 25