var array = {
obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
};
is not an array (its name notwithstanding). It is an object. The idea of sorting by obj3
and obj4
doesn't really make sense.
Now, if you were to convert this object to an array of objects, you could sort that array with the array.sort
method.
var array = [
{ obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
},{ obj1: 40,
obj2: 7,
obj3: 'text2'
obj4: 'text3'
obj5: 0
}
];
array.sort(function(a, b) {
var textA = a.obj3.toLowerCase();
var textB = b.obj3.toLowerCase();
if (textA < textB)
return -1;
if (textA > textB)
return 1;
return 0;
});
and of course to sort by a numeric property, it'd simply be:
array.sort(function(a, b) {
return a.obj1 - b.obj1;
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…