1

I want to sort columns in ng-repeat in AngularJs.

I have something like this:

<th class="cl_sort_init" ng-click="predicate='EstimatedCloseDate';
    reverse=!reverse" ng-class="{'cl_sort_desc': 
predicate=='EstimatedCloseDate'&&reverse==true, 'cl_sort_asc': 
predicate=='EstimatedCloseDate'&&reverse==false}">Close Date</th>

However EstimatedCloseDate is a string. Hence it doesnt work the way it should. How do I make it work as dates just for this column. Other columns are strings and they work just fine.

Any ideas and suggestions !!!

2 Answers 2

1

Depending on the format of the date string, you might be able to do

(new Date(EstimatedCloseDate))

This will parse the date string then convert it into a date object, which will play nice with sorting If this doesn't work, you will have to write your own date parser, which eventually turns the date into an int or Date object

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

2 Comments

Why worry about the extra conversion to integers? A date time should sort just fine.
@Paul Sasik oh nice I didn't know that :)
0

In your predicate you should be able to convert the string to date time. No other conversion should be needed. See here for string to date conversion: Converting string to date in js and format particulars.

If your date format is unusual check out libraries like datejs for help.

But here are a few snippets converting several date strings into dates. Notice that the last conversion fails.

var dtString = "2010-12-25";
var dt = new Date(dtString);
console.log(dtString + " = " + dt);

dtString = "12/25/2010";
dt = new Date(dtString);
console.log(dtString + " = " + dt);

dtString = "25/12/2010";
dt = new Date(dtString);
console.log(dtString + " = " + dt);

Output:

2010-12-25 = Fri Dec 24 2010 18:00:00 GMT-0600 (CST)
12/25/2010 = Sat Dec 25 2010 00:00:00 GMT-0600 (CST)
25/12/2010 = Invalid Date

4 Comments

I tried something like this: <th class="cl_sort_init" ng-click="predicate=controller.formatDate(EstimatedCloseDate);reverse=!reverse" ng-class="{'cl_sort_desc': predicate==controller.formatDate(EstimatedCloseDate) &&reverse==true, 'cl_sort_asc': predicate==controller.formatDate(EstimatedCloseDate) &&reverse==false}">Close Date</th>. It did not work. I have method called formatDate in the controller like this: formatDate:(date)=> return new Date(date)
Thanks for your reply. I tried it on similar lines you suggested. What am I doing wrong in the above snipped.
@SaiBand: Could you provide some example dates that you're passing into your formatDate function? It's possible that the format is not being recognized by the Date constructor. Also, please don't paste code into comments. It's very hard to read.
@SaiBand: If you see my code snippet in the edit you'll see that new Date() will work if it can recognize the format. So the format of your date string is your prime suspect for why the code isn't working.

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.