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.
Sorting by today was a little tricky to achieve just using sort. To overcome it we are creating an array of days, then resorting that so the current day is at index 0, then sorting your calendar array based on the sorted days.

to avoid having to calculate the `sortedDays` multiple times, it's wrapped in a closure that returns the function that will sort the given array.

It's immutable so you won't need to change the original order if you don't need to.

```js
const calendar=[{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"}];

const sortByToday = (function() {

  const date = new Date()
  const today = date.getDay()

  const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

  const sortedDays = [
    ...days.slice(today),
    ...days.slice(0, today)
  ]

  return function(calendar) {
    return calendar.slice().sort((a, b) => {
      return sortedDays.indexOf(a.day) > sortedDays.indexOf(b.day)
    })
  }
})()

console.log(
  sortByToday(calendar)
)
```