sorting dates using .$orderBy

11,348 views
Skip to first unread message

kumjin.choy

unread,
Nov 9, 2011, 8:06:56 AM11/9/11
to AngularJS
Hello,

I'm having problems in doing a date sorting using .$orderBy. Here is
an example of my situation. http://jsfiddle.net/rvdww/5/

The dates are sorted alphabetically if you notice, but this is not
something i want. Is there any way I can do this in angularjs that can
parse the date as date object and sort them correctly?

Another way I thought of is to create a date object and store the
primitive value in a data attribute(data-dateobj) using valueOf. And
later use predicate='dateobj' instead of predicate='date'

<tr ng:repeat="friend in friends.$orderBy(predicate, reverse)">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.age}}</td>
<td data-dateobj="{{friend.dateobj}}">{{friend.date}}</td>
<tr>

However, with this approach how do i go about doing it?

I've tried doing this but the sorting results still doesnt work.

this.predicate = function(friend) {
friend.dateobj = new Date(friend.date).valueOf();
return (friend);
};

appreciate if someone can help on this.

thanks!

Igor Minar

unread,
Nov 9, 2011, 10:41:59 AM11/9/11
to ang...@googlegroups.com
it should be quite easy to enhance the orderBy code to be date-aware.

can you file an rfe?

anyone in the community wants to work on this? I can walk you through the code.

/i




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Di Peng

unread,
Nov 9, 2011, 10:54:57 AM11/9/11
to ang...@googlegroups.com
Hi,
   I can help with that :)
   BTW how to file an rfe?
Di

Mårten Dolk

unread,
Nov 9, 2011, 11:13:51 AM11/9/11
to ang...@googlegroups.com
I think it worth pointing out that you can keep the dates as ISO 8601
strings which sort naturally and use Angular's date filter to display
the dates:

http://jsfiddle.net/P4fSZ/
http://docs.angularjs.org/#!/api/angular.filter.date


/Mårten

2011/11/9 Di Peng <pend...@gmail.com>:

Igor Minar

unread,
Nov 9, 2011, 12:01:49 PM11/9/11
to ang...@googlegroups.com
correct, but if the model contains date objects, we should not force a model transformation just so that it is rendered in the correct order by the view.

the view should be smarter and do the ordering properly.

/i

Mårten Dolk

unread,
Nov 9, 2011, 12:14:45 PM11/9/11
to ang...@googlegroups.com
I agree, but the fiddle in the original question did not contain any
date objects :-)

/mårten

2011/11/9 Igor Minar <ig...@angularjs.org>:

Vojta Jina

unread,
Nov 9, 2011, 9:14:40 PM11/9/11
to ang...@googlegroups.com
I can't see any issue guys...

Sorting, when model contain date objects works fine: http://jsfiddle.net/vojtajina/UtKRv/

The original fiddle uses string, I don't think we should inspect whether a string is date and treat it as date than...

V.

Igor Minar

unread,
Nov 10, 2011, 4:08:28 AM11/10/11
to ang...@googlegroups.com, pend...@gmail.com
oh. duh!

How come I didn't see that when I looked at the fiddle the first time?

You are right. If the model is a string then we should not try to do magical stuff with it and just sort it alphabetically.

Di, don't bother doing anything with this as it's not a bug or a useful feature.

/i



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/EW-i3hiYrB0J.

kumjin.choy

unread,
Nov 10, 2011, 8:11:51 AM11/10/11
to AngularJS
Vojta, you have a point there too. Initially I thought there might be
a out of the box solution for sorting dates, given that the dates are
stored as strings and not date objects.
It might end up an overkill to work on this rfe.

Anyway, I've managed to get my dates to be sorted out correctly by
getting the date into an object using Date.parse() or new Date().

Vojta Jina

unread,
Nov 10, 2011, 2:43:40 PM11/10/11
to ang...@googlegroups.com
You can pass function to $oderBy() and convert string into Date object there... http://jsfiddle.net/vojtajina/rvdww/6/

but it's much more efficient and better to store dates as Date objects when you need sorting them....

V.

Reply all
Reply to author
Forward
0 new messages