Created
October 31, 2012 12:44
-
-
Save Urigo/3986849 to your computer and use it in GitHub Desktop.
Changes to Angular JS to let you orderBy objects instead of just arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js | |
index 93f3f5a..ddf2552 100644 | |
--- a/src/ng/filter/orderBy.js | |
+++ b/src/ng/filter/orderBy.js | |
@@ -6,12 +6,12 @@ | |
* @function | |
* | |
* @description | |
- * Orders a specified `array` by the `expression` predicate. | |
+ * Orders a specified `input` by the `expression` predicate. | |
* | |
* Note: this function is used to augment the `Array` type in Angular expressions. See | |
* {@link ng.$filter} for more informaton about Angular arrays. | |
* | |
- * @param {Array} array The array to sort. | |
+ * @param {Array|Object} input The array or object to sort. | |
* @param {function(*)|string|Array.<(function(*)|string)>} expression A predicate to be | |
* used by the comparator to determine the order of elements. | |
* | |
@@ -25,8 +25,8 @@ | |
* - `Array`: An array of function or string predicates. The first predicate in the array | |
* is used for sorting, but when two items are equivalent, the next predicate is used. | |
* | |
- * @param {boolean=} reverse Reverse the order the array. | |
- * @returns {Array} Sorted copy of the source array. | |
+ * @param {boolean=} reverse Reverse the order the array or object. | |
+ * @returns {Array} Sorted array copy of the source input. | |
* | |
* @example | |
<doc:example> | |
@@ -88,9 +88,9 @@ | |
*/ | |
orderByFilter.$inject = ['$parse']; | |
function orderByFilter($parse){ | |
- return function(array, sortPredicate, reverseOrder) { | |
- if (!(array instanceof Array)) return array; | |
- if (!sortPredicate) return array; | |
+ return function(input, sortPredicate, reverseOrder) { | |
+ if (!(input instanceof Array) && !(input instanceof Object)) return input; | |
+ if (!sortPredicate) return input; | |
sortPredicate = isArray(sortPredicate) ? sortPredicate: [sortPredicate]; | |
sortPredicate = map(sortPredicate, function(predicate){ | |
var descending = false, get = predicate || identity; | |
@@ -105,9 +105,14 @@ function orderByFilter($parse){ | |
return compare(get(a),get(b)); | |
}, descending); | |
}); | |
- var arrayCopy = []; | |
- for ( var i = 0; i < array.length; i++) { arrayCopy.push(array[i]); } | |
- return arrayCopy.sort(reverseComparator(comparator, reverseOrder)); | |
+ var inputCopy = []; | |
+ if(input instanceof Array) { | |
+ for ( var i = 0; i < input.length; i++) { inputCopy.push(input[i]); } | |
+ } | |
+ else if(input instanceof Object) { | |
+ for ( var i in input) { inputCopy.push(input[i]); } | |
+ } | |
+ return inputCopy.sort(reverseComparator(comparator, reverseOrder)); | |
function comparator(o1, o2){ | |
for ( var i = 0; i < sortPredicate.length; i++) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment