Denis Nazarenko
Denis Nazarenko

Reputation: 164

How to sort array of objects

I need your help to understand how to sort array of objects with another array inside each of object. I have response from api with array bunch of objects like below:

[
    {
        "name": "[C US Equity] CITIGROUP INC",
        "stats": [
            {
                "label": "DTD",
                "value": null
            },
            {
                "label": "MTD",
                "value": null
            },
            {
                "label": "YTD",
                "value": 0.0536530913792681
            },
            {
                "label": 2016,
                "value": 0.18102519526139493
            },
            {
                "label": 2015,
                "value": -0.012946569977188238
            },
            {
                "label": 2014,
                "value": null
            }
        ]
    }...
]

I should be sort it by value of label "YTD" exclude of null value. Any help are welcome

Upvotes: 0

Views: 233

Answers (3)

siuga
siuga

Reputation: 31

I think this post may help you.
Take a look of it, it teaches you about how compare works.

All you need to do is to put that JSON thing into an object array, then compare the value of YTD(make two for-loops, result [i] .stats [j] .label === 'YTD') to make the array to re-order it.

A comparison function is work like below(extracted from the above link)

function CompareNumbers( v1, v2 ) {
    if ( v1 < v2 ) return -1;  // v1 goes nearer to the top
    if ( v1 > v2 ) return 1;   // v1 goes nearer to the bottom
    return 0;                  // both are the same
}

when it return -1(or sometimes false), the former one will be pushed to the front of your array, meanwhile 1 or true will push the former one to the back. And 0 (means not true or false, or you can handle the exception by yourself, push back or forward) will happen nothing.

But becare if your array exist two 'YTD' labelled value, if it happens it will make two value to appear in the same data.

This may help you in sorting the data while you cannot directly get a sorted data from your db server.
Oh, forgot to say, the compare function can compare the whole object.(of course you need to extract something like this ---> object[index].['Something your needed']), not only numbers.

Upvotes: 0

pixlboy
pixlboy

Reputation: 1502

You can use collection sort method provided by lodash. https://lodash.com/docs/4.17.4#sortBy

var users = [{
    "name": "[C US Equity] CITIGROUP INC",
    "stats": [
        {
            "label": "DTD",
            "value": null
        },
        {
            "label": "MTD",
            "value": null
        },
        {
            "label": "YTD",
            "value": 0.0536530913792681
        },
        {
            "label": null,
            "value": 0.18102519526139493
        },
        {
            "label": "2015",
            "value": -0.012946569977188238
        },
        {
            "label": "dtd",
            "value": null
        }
    ]
}]

console.log(_.sortBy(users[0].stats, [function(o) { if(o.label){return  o.label.toUpperCase();} }]));

This takes case of null values too.

Upvotes: 0

brk
brk

Reputation: 50291

You can use array#sort method

var oldArray = [{
  "name": "[C US Equity] CITIGROUP INC",
  "stats": [{
      "label": "DTD",
      "value": null
    },
    {
      "label": "MTD",
      "value": null
    },
    {
      "label": "YTD",
      "value": 0.0536530913792681
    },
    {
      "label": 2016,
      "value": 0.18102519526139493
    },
    {
      "label": 2015,
      "value": -0.012946569977188238
    },
    {
      "label": 2014,
      "value": null
    }
  ]
}];
oldArray[0].stats.sort(function(a, b) {
  return a.label - b.label;

})
console.log(oldArray)

Upvotes: 1

Related Questions