1
var a = [
   {  
        "subObj1": {  
            "key1":10722905,
            "key2":"0080817626"
        },
        "outerKey1":"abcd",
        "outerKey2":"defg"
    },
    {  
        "subObj1": {  
            "key1":123456,
            "key2":"0987654"
        },
        "outerKey1":"pqrs",
        "outerKey2":"ased"
    }
]

i need to sort this array of object using key1 and outerkey1 both separately. That logic is already implemented.

_.sortBy(a,'outerKey1') -> this is working fine.

_.sortBy(a,'key1') -> this is not working.

_.sortBy(a,'subObj1.key1') -> this is also not working.

Is there any way to sort this array of objects with inner key of object like key1 or key2 using lodash _.sortBy function?

4
  • 1
    Please show us what you have tried so far Commented May 9, 2017 at 9:04
  • @Sagar I am new to lodash. I have tried with _.sortBy by giving different parameters as key1 and subObj1.key1..But its returning me the same object without sorted. Commented May 9, 2017 at 9:07
  • Use listjs.com, it is Tiny, invisible and simple, yet powerful and incredibly fast vanilla JavaScript that adds search, sort, filters and flexibility to plain HTML lists, tables, or anything. Commented May 9, 2017 at 9:07
  • a.sort((a,b)=> a.subObj1.key1 - b.subObj1.key1) Commented May 9, 2017 at 9:09

1 Answer 1

1

Using only JavaScript you can use Array.prototype.sort() along with a custom callback fucntion:

var sorted = arr.sort(function(a, b) {
  a.subObj1.key1 - b.subObj1.key1
});

Demo:

var arr = [{
    "subObj1": {
      "key1": 10722905,
      "key2": "0080817626"
    },
    "outerKey1": "abcd",
    "outerKey2": "defg"
  },
  {
    "subObj1": {
      "key1": 123456,
      "key2": "0987654"
    },
    "outerKey1": "pqrs",
    "outerKey2": "ased"
  }
];

var sorted = arr.sort(function(a, b) {
  a.subObj1.key1 - b.subObj1.key1
});

console.log(sorted);

Edit:

If you you wnat to do it with lowdash, you can use _.sortBy() with a custom sorter function:

var customSorter = function(obj) {
  return obj.subObj1.key1;
};
var sorted = _.sortBy(arr, customSorter);

Demo:

var arr = [{
    "subObj1": {
      "key1": 10722905,
      "key2": "0080817626"
    },
    "outerKey1": "abcd",
    "outerKey2": "defg"
  },
  {
    "subObj1": {
      "key1": 123456,
      "key2": "0987654"
    },
    "outerKey1": "pqrs",
    "outerKey2": "ased"
  }
];

var customSorter = function(obj) {
  return obj.subObj1.key1;
};
var sorted = _.sortBy(arr, customSorter);

console.log(sorted);
<script src="http://underscorejs.org/underscore-min.js"></script>

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

4 Comments

Thanks for the quick answer but I am already using lodash for sorting on other parameters in kind of same function. I dont want to change whole logic only for this sort.Is there any way to do it using lodash?
@RoshanJoshi don't rely on libraries man, javascript does it all, get the app Kapeli Dash and just look up Array.prototype functions which are inherited fallback functions that all arrays have
@neaumusic thanks for the suggestion. I was using that library at many places so didnt want to change it only for one place change. Will keep this in mind.
@chsdk thanks for the custom sorter.Works totally fine.

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.