Skip to content

Instantly share code, notes, and snippets.

@kanayannet
Created July 14, 2011 13:16
Show Gist options
  • Save kanayannet/1082425 to your computer and use it in GitHub Desktop.
Save kanayannet/1082425 to your computer and use it in GitHub Desktop.
<html>
<head>
<script language=javascript>
<!--
var by = function(name)
{
return function(o,p)
{
var a,b;
if(typeof o === 'object' && typeof p === 'object' && o && p)
{
a = o[name];
b = p[name];
if(a === b)
{
return 0;
}
if(typeof a === typeof b)
{
return a < b ? -1 : 1;
}
return typeof a < typeof b ? -1 : 1;
}else
{
throw {
name: 'Error',
message: name + 'を含むオブジェクトが必要です。'
};
}
};
};
var s = [
{first: 'Joe',last: 'Besser'},
{first: 'Moe',last: 'Howard'},
{first: 'Joe',last: 'DeRita'},
{first: 'Shemp',last: 'Howard'},
{first: 'Larry',last: 'Fine'},
{first: 'Curly',last: 'Howard'}
];
s = s.sort(by('first'));
for(var i=0;i<s.length;i+=1)
{
alert(s[i].first);
}
//-->
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment