caetus,
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title> <meta charset="utf-8">
</head>
<body>
<script>
var o = [{
email: 'js@.ru',
username: {
first: 'Bob',
last: 'Fox'
},
dob: '12.02.2002'
},{
email: 'kiss@.ru',
username: {
first: 'Alex',
last: 'Fox'
},
dob: '12.02.2002'
} ,{
email: 'a@.ru',
username: {
first: 'Mi',
last: 'Fox'
},
dob: '12.02.2002'
}]
function f(arr, key, up) {
var e = function(b) {
return function(a) {
var c = 0;
for (a = a[b[c++]]; c < b.length; c++) a = a[b[c]];
return a
}
}(key);
return arr.sort(function(b, a) {
b = e(b);
a = e(a);
return b > a ? up ? -1 : 1 : b < a ? up ? 1 : -1 : 0
});
};
document.write('<p>сортировка по email ⇓</p><br>')
document.write(JSON.stringify(f(o,['email'])));
document.write('<p>сортировка по email ⇑</p><br>')
document.write(JSON.stringify(f(o,['email'], true)));
document.write('<p>сортировка по first ⇓</p><br>')
document.write(JSON.stringify(f(o,['username','first'])));
document.write('<p>сортировка по first ⇑</p><br>')
document.write(JSON.stringify(f(o,['username','first'],true)));
</script>
</body>
</html>