1

Let's say that I have an array of objects like so

var outer = [
  {
    "name": "T1",
    "inner": [
      {
        "type": "DAY"
      },
      {
        "type": "MONTH"
      },
      {
        "type": "WEEKLY"
      }
    ]
  },
  {
    "name": "T2",
    "inner": [
      {
        "type": "DAY"
      },
      {
        "type": "MONTH"
      },
      {
        "type": "WEEKLY"
      }
    ]
  }
];

I'm basically trying to sort the objects in the inner array so that the 'type' is in this order - MONTH, WEEKLY, DAY. Like this.

"inner": [
          {
            "type": "MONTH"
          },
          {
            "type": "WEEKLY"
          },
          {
            "type": "DAY"
          }
        ]

Is there a way to do this using Lo-dash? Here's a small fiddle for this.

I saw a similar question here, but this is not exactly what I was looking for because in my case, the order will be fixed, and sort will not be based on a random string value.

4
  • Where are you stuck? The answers to the question you linked clearly demonstrate how to use Array#sort (which is also well-documented elsewhere). So, what have you tried? All that's required in a sort callback is to return less than zero if the first thing should be before the second, 0 if they're the same, and greater than zero if the first thing should come after the second. Commented Apr 14, 2016 at 6:13
  • The issue I get with using Array#sort is that it gives me either an ascending or a descending order. But I'm still not sure how to sort the objects using a predefined order. I referred this answer here. Commented Apr 14, 2016 at 6:32
  • So you figure out how to return the right value. Commented Apr 14, 2016 at 6:35
  • So, I eventually took your suggestion and gurvinder372's answer. You were correct, does not make sense to use a library for what is already built-in. But I was curious if there was a lodash way of doing it. Thanks for the help, guys. Commented Apr 14, 2016 at 16:37

1 Answer 1

2

try this

var priority = [ "MONTH", "WEEKLY", "DAY" ];

outer.forEach(function(obj){
  obj.inner.sort(function(a,b){
    var ap = priority.indexOf(a.type);
    var bp = priority.indexOf(b.type);
    return ap-bp;
  });
}); 

An alternative approcah and I guess more cleaner approach (thanks to T.J. Crowder :) )

var priority = {MONTH: 0, WEEKLY: 1, DAY: 2};

outer.forEach(function(obj){
  obj.inner.sort(function(a,b){
    var ap = priority[a.type];
    var bp = priority[b.type];
    return ap-bp;
  });
}); 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this does work. However, is there a way to do this using via lodash only? Can I use _.sortBy for this?
Surely if you want to map the strings to values, an object would make more sense? var priority = {MONTH: 0, WEEKLY: 1, DAY: 2}; and then return priority[a] - priority[b];
@Misaal: Why reach for a library function to do something that's already built-in?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.