Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created May 5, 2009 21:37
Show Gist options
  • Save thomaslang/107222 to your computer and use it in GitHub Desktop.
Save thomaslang/107222 to your computer and use it in GitHub Desktop.
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:
if (v1<v2) return -1;
if (v1>v2) return 1;
return 0;
break;
case SC.T_NUMBER:
if (v1<v2) return -1;
if (v1>v2) return 1;
return 0;
break;
case SC.T_STRING:
if (v1.localeCompare(v2)<0) return -1;
if (v1.localeCompare(v2)>0) return 1;
return 0;
break;
case SC.T_ARRAY:
var l = Math.min(v1.length,v2.length);
var r = 0;
var i = 0;
while (r==0) {
r = this.compareObjects(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