1

I have an array of objects response from the server, I am trying to sort by uid in ascending order, while keeping the entire array intact for the data array. Meaning in the data array there are 2 elements. I am trying to look at the uid in one of the arrays it is in of all the elements within the array and determine the order of how the elements should be placed within the data array.

I have tried the following using lodash, but it doesn't seem to work, but I feel I'm getting close and would like some assistance:

// sort by:
console.log(res.results[0].data[0].row[1].uid)

_.forEach(res.results, function(obj) {
  _.forEach(data, function(anotherObj) {
    _.forEach(row, function(row) {
      data.row = _.sortBy(data.row, function(row) {
      return row[1].uid;
      });
    });
  });
});

Here is the array of objects returned from server:

var res = {
  results: [{
    columns: ['large', 'medium', 'small'],
    data: [{
      row: [{
        sid: 13,
        tid: 427
      },
        {
          uid: 69,
          vid: 450,
          wid: 65
        },
        null],
      multirow: {
        nodule: [{
          xid: '427',
          properties: {
            yid: 13,
            zid: 427
          }
        },
          {
            aid: '450',
            properties: {
              uid: 69,
              bid: 450,
              cid: 65
            }
          }]
      }
    },
      {
        row: [{
          sid: 13,
          tid: 427
        },
          {
            vid: 432,
            uid: 65,
            wid: 61
          },
          null],
        multirow: {
          nodule: [{
            xid: '427',
            properties: {
              yid: 13,
              zid: 427
            }
          },
            {
              aid: '432',
              properties: {
                bid: 432,
                uid: 65,
                cid: 61
              }
            }]
        }
      }]
  }],
  errors: []
};
3
  • Your row arrrays have 3 elements each, only 2 of them are objects, and only 1 of them has an uid property. What exactly do you want to sort, the row arrays, the data array(s) or the results array? Commented Apr 21, 2016 at 23:40
  • The variables in your code are totally unconnected. I guess you meant obj.data instead of data, anotherObj.row instead of row, and uh, that inner loop doesn't appear to make any sense at all… Commented Apr 21, 2016 at 23:42
  • @Bergi, thanks for pointing out my typo. I will fix the question. What I am trying to do is sort the elements within the data array by uid. And yes you are correct uid is in 2 places row and multirow. But I was just going to use row instead of multirow Commented Apr 21, 2016 at 23:45

1 Answer 1

1

You don't need that many loops:

_.forEach(res.results, function(result) {
  result.data = _.sortBy(result.data, function(datum) {
    return datum.row[1].uid;
  });
});

You only need to iterate over the results, each of which might have a data array that you are going to sort. The sortBy callback then takes each of the elements of that array and is supposed to return the value to compare.

Sign up to request clarification or add additional context in comments.

2 Comments

Just tried what you proposed, in jsfiddle and it appeared to work. yeah, I dunno why, I felt I needed more loops to get into that array. I guess lodash does more heavy lifting than I expected.
You've got three levels of arrays, yes, but: One you are iterating, one you are sorting, and one you are accessing directly with an index (I'm not even sure whether this should be an array at all).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.