0

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

Table Demo

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"
    }
  ]
10
  • I don't use Angular, but I'd expect a custom sort comparator to take two arguments similar to using one with the standard array .sort(). Commented Oct 23, 2017 at 3:20
  • can you provide holidays array ? Commented Oct 23, 2017 at 3:40
  • @Akashii I have updated the question with all the details. Commented Oct 23, 2017 at 4:05
  • Yeah I have tried and I am getting as NaN Commented Oct 23, 2017 at 4:16
  • @Batman In fiddle I am not able to see your JavaScript code. Why? Commented Oct 23, 2017 at 4:23

1 Answer 1

2

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 :

http://plnkr.co/edit/afgsPDBF5RXG5WviYrdU?p=preview

Hope it help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.