Question: I am trying to re-order the array below given todays date:

```
[
    {
        "index": "0",
        "day": "Monday",
        "food": "Salad"
    },
    {
        "index": "1",
        "day": "Friday",
        "food": "Pasta"
    },
    {
        "index": "2",
        "day": "Sunday",
        "food": "Pasta"
    },
    {
        "index": "3",
        "day": "Thursday",
        "food": "Pasta"
    },
    {
        "index": "4",
        "day": "Tuesday",
        "food": "Pasta"
    },
    {
        "index": "2",
        "day": "Sunday",
        "food": "Pasta"
    },
    {
        "index": "5",
        "day": "Wednesday",
        "food": "Pasta"
    },
    {
        "index": "6",
        "day": "Saturday",
        "food": "Pasta"
    }
]

```

Say today is `Saturday`, after ordering, the expected output by item index is: 6, 2, 2, 0, 4, 5, 3, 1
If today was `Tuesday`, the output by index is: 4, 5, 3, 1, 6, 2, 2, 0

I am using `moment.js`, and so far I am failing to create a non bulky code for this.
Eventually I would like to wrap the code in a custom orderBy filter to be used on a ng-repeat directive.

Answer: 
Here's the minimal solution I came up with:

```
const DAYS = { 'Sunday': 0, 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6 };

// Get the relative index of a day using today as a reference.
function relativeIndex(day) {
  const TODAY = new Date().getDay();
  return (DAYS[day] - TODAY + 7) % 7;
}

// Sort a lit of data starting by today.
function sort(data) {
  return data.sort((a, b) => relativeIndex(a.day) - relativeIndex(b.day));
}

```

The key here is the `relativeIndex` function which calculates the index of any day using today as a reference.
Given 'menu' as given array, use customized comparison to sort:

```
var map = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = (new Date()).getDay();
menu.sort(function(a, b){
    var aDay = map.indexOf(a.day), bDay = map.indexOf(b.day);
    return (aDay - bDay) * 
        (day <= Math.min(aDay, bDay) || day > Math.max(aDay, bDay) ? 1 : -1);
});

for(var i=0; i<menu.length; ++i){
  console.log(menu[i].index);
}

```