1

I sort collection of objects with single primary key:

mydata = _.sortBy(mydata, function (obj) {
    return parseInt(obj[pk], 10);
});

But I cannot sort this numeric strings when is a composite key like ["RHID","CD_DOC_ID","SEQ"]

5
  • Please provide a sample of mydata, and the expected output. Commented Jan 18, 2016 at 16:12
  • try _.sortBy(mydata); Commented Jan 18, 2016 at 16:12
  • mydata is array of objects. here is an object { "RHID": "1", "CD_DOC_ID": "1", "SEQ": "1", "NR_DOCUMENTO": "3333", "EMISSOR": "Lisboas643223", "DT_EMISSAO": "2004-01-12", "DT_VALIDADE": "2014-12-17", "NOME_FICHEIRO": "1EV5.pdf" } and i want to sort by the first tree propertie that are an array Commented Jan 18, 2016 at 17:43
  • The problem is that the values are strings Commented Jan 18, 2016 at 17:49
  • you want to sort your { "RHID": "1", "CD_DOC_ID": "1", "SEQ": "1", "NR_DOCUMENTO": "3333", "EMISSOR": "Lisboas643223", "DT_EMISSAO": "2004-01-12", "DT_VALIDADE": "2014-12-17", "NOME_FICHEIRO": "1EV5.pdf" } object with ["RHID","CD_DOC_ID","SEQ"] array? Commented Jan 19, 2016 at 4:58

1 Answer 1

5

In Lodash v3 do:

mydata = _.sortByAll(
  mydata, 
  [
    function (obj) {
      return parseInt(obj["RHID"], 10);
    },
    function (obj) {
      return parseInt(obj["CD_DOC_IC"], 10);
    },
    function (obj) {
      return parseInt(obj["SEQ"], 10);
    }
  ]
);

In Lodash v4: replace _.sortByAll with _.sortBy

If you also want to specify the sorting direction, use _.sortByOrder (v3) or _.orderBy (v4) instead.

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

Comments

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.