Skip to content
Browse files

Nested sort by Dean Landolt

  • Loading branch information...
1 parent 560bc79 commit 01c10845a37c9474bcb59f9d1e883a2451fc5a37 @kriszyp kriszyp committed Feb 16, 2010
Showing with 20 additions and 9 deletions.
  1. +20 −9 lib/resource-query.js
View
29 lib/resource-query.js
@@ -91,17 +91,28 @@ exports.QueryFunctions = function(){
}
exports.QueryFunctions.prototype = {
- sort: function(sortAttribute){
- var firstChar = sortAttribute.charAt(0);
- var ascending = true;
- if(firstChar == "-" || firstChar == "+"){
- if(firstChar == "-"){
- ascending = false;
- }
- sortAttribute = sortAttribute.substring(1);
+ sort: function(){
+ var terms = [];
+ for(var i = 0; i < arguments.length; i++){
+ var sortAttribute = arguments[i];
+ var firstChar = sortAttribute.charAt(0);
+ var term = {attribute: sortAttribute, ascending: true};
+ if (firstChar == "-" || firstChar == "+") {
+ if(firstChar == "-"){
+ term.ascending = false;
+ }
+ term.attribute = term.attribute.substring(1);
+ }
+ terms.push(term);
}
this.sort(function(a, b){
- return ascending == a[sortAttribute] > b[sortAttribute] ? 1 : -1;
+ for (var i = 0; i < terms.length; i++) {
+ var term = terms[i];
+ if (a[term.attribute] != b[term.attribute]) {
+ return term.ascending == a[term.attribute] > b[term.attribute] ? 1 : -1;
+ }
+ }
+ return true; //undefined?
});
return this;
},

0 comments on commit 01c1084

Please sign in to comment.
Something went wrong with that request. Please try again.