1

I have a data which contains employment history. I have two fields for my employment dateTo:

yearTo, monthTo

I need to sort the employment history correctly based on the most recent employment dateTo which I can do by

<tr id="employment_history_" ng-repeat="history in candidate.employmentHistory | orderBy : ['-yearTo','monthTo',] | date:['YYYY', 'MMM'] ">

It works correctly. But I have one problem. The field monthTo can also containt a value Present instead of just month date format MMM like JAN, FEB MAR etc.

Question how do I sort this, I understand I have a date format but I want the record which has the monthTo value Present to first on the list?

4
  • You are asking how to handle Present as a value in order by right? Commented Mar 2, 2017 at 6:41
  • @YogeshPatil yes. together with the dates Commented Mar 2, 2017 at 6:59
  • Here is a jsfiddle that should give you some steps in the right direction. jsfiddle.net/2yt04ztr/13 Commented Mar 2, 2017 at 7:22
  • @GiovaniVercauteren thanks it worked. You may want to post your comment as answer? Commented Mar 2, 2017 at 8:10

1 Answer 1

1

So this JSFiddle can help you in the right direction: JSFiddle

The way this works is because you can give the orderBy directive an array of predicates, like you did in your provided example. In your example however you only used string predicates while you can also use function predicates. The function predicate will receive the item you're trying to order and you can choose what you want to return so angular can use that value to order the value.

ng-repeat="hist in main.history | orderBy: ['-yearTo', main.orderMonthTo]"

this.orderMonthTo = function(item) {
    if (item.monthTo === 'Present') {
    return -13;
  } else {
    return Number(moment(item.monthTo, 'MMM').format('M'))*-1;
  }
}

More information can be found about this here. Also, in the JSFiddle I used the momentjs library to make it easier to work with dates.

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.