sort实现

2020-10-28 10:32:44 浏览数 (1)

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
		/*let arr=["c","b","a"];
		arr.sort();//从小到大排序
		console.log(arr);
		/*arr.sort(function(a,b)
			{
				if(a>b)
				{
					return 1;
				}
				else if(a<b)
				{
					return -1;
				}
				else
				{
					return 0;
				}
			});
			*/
		/*let arr1=[1,2,3];
		arr1.sort(function(a,b)
			{
				return b-a;
			});
		console.log(arr1);*/
		/*let arr2=["1234","21","54321","6"];
		arr2.sort(function(a,b)
			{
				//return  b.length-a.length;
				return a.length-b.length;
			});
		console.log(arr2);*/
		let q=[{name:"cyg",age:34},{name:"cyg",age:341},{name:"cyg",age:343},{name:"cyg",age:342}];
		q.sort(function(a,b)
			{
				//return a.age-b.age;
				return b.age-a.age;
			});console.log(q);
	</script>
</body>
</html>

0 人点赞