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 !!!

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
SaiBand
  • 5,025
  • 15
  • 57
  • 76

2 Answers2

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

XrXr
  • 2,027
  • 1
  • 14
  • 20
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
Community
  • 1
  • 1
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • I tried something like this: Close Date. It did not work. I have method called formatDate in the controller like this: formatDate:(date)=> return new Date(date) – SaiBand Jun 20 '14 at 00:56
  • Thanks for your reply. I tried it on similar lines you suggested. What am I doing wrong in the above snipped. – SaiBand Jun 20 '14 at 00:58
  • @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. – Paul Sasik Jun 20 '14 at 02:35
  • @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. – Paul Sasik Jun 20 '14 at 02:54