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.

Community
  • 1
  • 1
rodiwa
  • 1,690
  • 2
  • 17
  • 35
  • 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. – T.J. Crowder Apr 14 '16 at 06: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](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects). – rodiwa Apr 14 '16 at 06:32
  • So you figure out how to return the right value. – T.J. Crowder Apr 14 '16 at 06: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. – rodiwa Apr 14 '16 at 16:37

1 Answers1

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;
  });
}); 
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Yes, this does work. However, is there a way to do this using via lodash only? Can I use _.sortBy for this? – rodiwa Apr 14 '16 at 06:34
  • 2
    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];` – T.J. Crowder Apr 14 '16 at 06:34
  • @Misaal: Why reach for a library function to do something that's already built-in? – T.J. Crowder Apr 14 '16 at 06:34