Currently, I have created the custom function to created to find the fromNow from the Moment and I want to sort the data based on the value returned by the function.
I want to put the Table Element at the Top for which Days from Now is lesser
In the below table 31 Days From Today at the 2nd column is lesser and I want to put this element at the top, I have tried below way but it's not working
Can someone help me Table
AngularJS OrderBy
<tr ng-repeat="x in Holidays|orderBy:findFromNow">
<td>{{$index+1}}</td>
<td>{{x.Day}}</td>
<td>
<span class="label label-warning">{{findFromNow(x.Date)}} Days From Today</span></td>
</tr>
Function
$scope.findFromNow = function (inputDate) {
var m = moment(inputDate, "D-MMM-YY");
var today = moment().startOf('day');
var days = Math.round((today - m) / 86400000);
if (days > 0) {
return 999;
} else {
return (days * (-1));
}
}
Holidays JSON
[
{
"Day": "Monday",
"Date": "2-Jan-17"
},
{
"Day": "Monday",
"Date": "20-Feb-17"
},
{
"Day": "Monday",
"Date": "29-May-17"
},
{
"Day": "Monday",
"Date": "3-Jul-17"
},
{
"Day": "Tuesday",
"Date": "4-Jul-17"
},
{
"Day": "Monday",
"Date": "4-Sep-17"
},
{
"Day": "Thursday",
"Date": "23-Nov-17"
},
{
"Day": "Friday",
"Date": "24-Nov-17"
},
{
"Day": "Monday",
"Date": "25-Dec-17"
}
]
You should write seperate function or check type of inputDate before use moment , because in orderBy , inputDate is Object not Date
$scope.sortDate = function(inputDate){
var m = moment(inputDate.Date, "D-MMM-YY");
var today = moment().startOf('day');
var days = Math.round((today - m) / 86400000);
if (days > 0) {
return 999;
} else {
return (days * (-1));
}
}
Here is plnkr :
Hope it help