-
-
Save geoffreyd/107260 to your computer and use it in GitHub Desktop.
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
compareObjects: function (v1, v2) { | |
var orderDefinition = [SC.T_ERROR, SC.T_UNDEFINED, SC.T_NULL, SC.T_BOOL, SC.T_NUMBER, SC.T_STRING, SC.T_ARRAY, SC.T_HASH, SC.T_OBJECT, SC.T_FUNCTION, SC.T_CLASS]; | |
//function getType (v) { | |
// var t = typeof v; | |
// if (t == 'object') { | |
// if (t == null) return 'null'; | |
// if (t instanceof Array) return 'array'; | |
// } | |
// return t; | |
//}; | |
var type1 = SC.typeOf(v1); | |
var type2 = SC.typeOf(v2); | |
if (orderDefinition.indexOf(type1) < orderDefinition.indexOf(type2)) return -1; | |
if (orderDefinition.indexOf(type1) > orderDefinition.indexOf(type2)) return 1; | |
// ok - types are equal - so we have to check inside types now | |
switch (type1) { | |
case SC.T_BOOL: | |
case SC.T_NUMBER: | |
if (v1<v2) return -1; | |
if (v1>v2) return 1; | |
return 0; | |
break; | |
case SC.T_STRING: | |
return v1.localeCompare(v2); | |
break; | |
case SC.T_ARRAY: | |
var l = Math.min(v1.length,v2.length); | |
var r = 0; | |
var i = 0; | |
while (r==0) { | |
r = arguments.callee(v1[i],v2[i]); | |
if ( r != 0 ) return r; | |
i++; | |
}; | |
break; | |
default: | |
return 0; | |
}; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment