Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs(orderBy): Clarify behavior of default comparator #15304

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/content/guide/migration.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,16 @@ or simply use:
As communicated before, IE8 is no longer supported.


### Filters (`orderBy`)

- due to [a097aa95](https://github.com/angular/angular.js/commit/a097aa95b7c78beab6d1b7d521c25f7d9d7843d9),
the default orderBy comparator sorts null values (strings, objects, etc.) as if they were the string `'null'`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null values can't be strings, objects etc. A null value is just null :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm...actually things are a little more complicated: #15293 (comment)

For this breaking change note, you need to mention the change introduced in a097aa9.
For the docs, you need to mention the current situation, as introduced in 48e1f56.

a null string will be sorted as if it started with the letter N. To sort nonexistent items to the last (or first,
if sort order is reversed) positions in the sort results, the sort predicate should use the empty string instead
of null. Similarly, predicate values that are `undefined` will be sorted as if they were strings starting with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this applies to undefined. The change only affects null afaict.

the letter U.





Expand Down
3 changes: 3 additions & 0 deletions src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
*
* **Note:** If you notice numbers not being sorted as expected, make sure they are actually being
* saved as numbers and not strings.
* **Note:** If null or undefined values are not being sorted as expected, convert them to the empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this note should mention is that null is treated as the string 'null' while sorting. There doesn't need to be a special mention about undefined, since it follows the algorithm described above.

I would also refrain from suggesting any work arounds, because there are different ones available, each with its pros and cons (e.g. use an extra predicate to separate null from non-null value, convert null to an empty string or undefined, use a custom comparator etc).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed as suggested.

* string before sorting. This is because of rule number 1 above which sorts `null` as `'null'`
* and `undefined` as `'undefined'`.
*
* @param {Array|ArrayLike} collection - The collection (array or array-like object) to sort.
* @param {(Function|string|Array.<Function|string>)=} expression - A predicate (or list of
Expand Down