<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>ObjectArraySort</title>
<script type="text/javascript">
window.onload = function(){
var objs = [{x:3,y:123},{x:2,y:56},{x:0,y:79},{x:1,y:29}];
objs.sort(compareTo);
console.log(objs);// [Object { x=0, y=79}, Object { x=1, y=29}, Object { x=2, y=56}, Object { x=3, y=123}]
};
function compareTo(a,b){
return a.x > b.x ? 1 : (a.x < b.x ? -1 : 0);//从左到右,升序
}
</script>
</head>
<body>
</body>
</html>