array.$orderBy(predicate) does not take an Array#sort function?

1,965 views
Skip to first unread message

Abed

unread,
Mar 4, 2011, 4:59:15 PM3/4/11
to ang...@googlegroups.com
Getting back results from an XHR several attributes I wanted to sort by are returned as strings instead of numbers of some sort, thus when sorting them you'd get undesired ordering e.g. 1, 20, 3, 4, ... I worked around this by turning them into actual numbers or floats depending on the attribute, however, initially I tried to use $orderBy(predicate) where predicate was a comparing function. E.g. (none of this is actual code, but the general idea)

within the Controller:

sortmethod = function(a, b){
console.log('a is ', a);
console.log('b is ', b);
 [...] 
}

In the HTML I'd set the predicate to sortmethod, e.g.
<tr ng:repeat="Customer in results.Customers.$orderBy(predicate)">

b would *always* return undefined. I don't know if I was doing something wrong, but it seemed to me it just wasn't using a sort function as documented. Can anyone confirm for me or correct me?

Thanks.

p.s. angularjs is amazing. It's really great work. Hope to learn to use it better/as intended.

Misko Hevery

unread,
Mar 4, 2011, 7:04:41 PM3/4/11
to ang...@googlegroups.com, Abed
Could you but together a working HTML page and the email it to us. I think what you are trying to do should work.

-- Misko

--
You received this message because you are subscribed to the Google Groups "Angular" 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.

Abed

unread,
Mar 5, 2011, 4:09:41 PM3/5/11
to ang...@googlegroups.com, Abed
You're right, I tried it elsewhere and it works consistently. I could not reproduce the problem I had outside of that given code. Just sent an e-mail. 

Misko Hevery

unread,
Mar 6, 2011, 1:59:23 AM3/6/11
to ang...@googlegroups.com, Abed
you are getting the &gt; because you have them in your template
<td>{{Customer.FirstName}}></td> 

The $orberBy problem is actual a problem with the documentation. Opps! corrected documentation included the corrected here. but your predicate is not a  function(a,b) but function(a) which returns the value you want to sort on. In your case it would be

function(a) {
  return Number(a.ListID);
}

you are doing the type conversion and you let the orderBy do the sorting.

-- Misko


angular.Array.orderBy

Description

Orders array by the expression predicate.

Note: this function is used to augment the Array type in angular expressions. See angular.Array for more info.

Usage

1

angular.Array.orderBy(array, expression[, reverse]);

Parameters

  • array – {Array} – The array to sort.
  • expression – {function(*)|string|Array.<(function(*)|string)>} – A predicate to be used by the comparator to determine the order of elements. Can be one of:
    • function: getter function. The result of this function will be sorted using the <, =, > operator
    • string: angular expression which evaluates to an object to order by, such as 'name' to sort by a property called 'name'. Optionally prefixed with + or - to control ascending or descending sort order (e.g. +name or -name).
    • Array: array of function or string predicates, such that a first predicate in the array is used for sorting, but when the items are equivalent next predicate is used.
  • reverse(optional) – {boolean} – Reverse the order the array.

Returns

{Array} – Sorted copy of the source array.


On Sat, Mar 5, 2011 at 1:09 PM, Abed <vers...@gmail.com> wrote:
You're right, I tried it elsewhere and it works consistently. I could not reproduce the problem I had outside of that given code. Just sent an e-mail. 

--

Abed Islam

unread,
Mar 6, 2011, 2:22:36 AM3/6/11
to Misko Hevery, ang...@googlegroups.com
Thanks for being patient with me, esp. with minute errors like me leaving an extra operand in there and not noticing it.

Oh that's very interesting. Then how come on one of the pages we are actually getting a function(a, b), granted both a and b are identical, whereas in another we are getting as you say function(a)? Nevertheless this really clarifies things. I can go and clean up the code tomorrow with that. Thanks again =)

Misko Hevery

unread,
Mar 6, 2011, 1:09:53 PM3/6/11
to ang...@googlegroups.com, Abed Islam
i am not sure which function(a,b) you are refereeing to, again, example would help. 

Abed

unread,
Mar 9, 2011, 4:11:37 PM3/9/11
to ang...@googlegroups.com, Abed Islam
Just wanted to say for the record that errors were by and far on me and that angularjs works as intended once the documentation is updated, i.e. Array.$orderBy  takes function(*) with 1 argument which returns the value to be sorted, as opposed to a function(*, *) with 2 arguments for comparison/handling the sorting. Once that's understood there's nothing at all wrong with Array.$orderBy......but plenty wrong with me w/o enough sleep haha. Kudos to Misko for being patient w/ me =D

Misko Hevery

unread,
Mar 9, 2011, 9:40:29 PM3/9/11
to ang...@googlegroups.com, Abed
May I suggest writing some automated test? :-) JavaScript without unit-tests can be a nightmare.

On Wed, Mar 9, 2011 at 1:11 PM, Abed <vers...@gmail.com> wrote:
Just wanted to say for the record that errors were by and far on me and that angularjs works as intended once the documentation is updated, i.e. Array.$orderBy  takes function(*) with 1 argument which returns the value to be sorted, as opposed to a function(*, *) with 2 arguments for comparison/handling the sorting. Once that's understood there's nothing at all wrong with Array.$orderBy......but plenty wrong with me w/o enough sleep haha. Kudos to Misko for being patient w/ me =D

--

Igor Minar

unread,
Mar 10, 2011, 2:08:12 AM3/10/11
to ang...@googlegroups.com, Misko Hevery, Abed
> JavaScript without unit-tests can be a nightmare

Can be? IMO it's a suicide.

/i

Misko Hevery

unread,
Mar 10, 2011, 10:58:32 AM3/10/11
to ang...@googlegroups.com, Igor Minar, Abed
Check out this seed app: https://github.com/angular/angular-seed

Read the README on how to run the tests.
Reply all
Reply to author
Forward
0 new messages