Created
September 20, 2016 23:59
-
-
Save anonymous/6f5d1178c9f83be170b91a78f86955d6 to your computer and use it in GitHub Desktop.
JS Bin [美团面试两列排序] // source https://jsbin.com/mejapis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="[美团面试两列排序]"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var arr = [ | |
{price:18, rank:5}, | |
{price:8, rank:4}, | |
{price:12, rank:2}, | |
{price:12, rank:4}, | |
{price:8, rank:3}, | |
{price:15, rank:5}, | |
{price:20, rank:4}, | |
{price:9, rank:1} | |
] | |
var res = arr.sort(function(a,b){ | |
return a.price != b.price ? a.price - b.price : b.rank - a.rank; | |
}) | |
console.log(arr); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var arr = [ | |
{price:18, rank:5}, | |
{price:8, rank:4}, | |
{price:12, rank:2}, | |
{price:12, rank:4}, | |
{price:8, rank:3}, | |
{price:15, rank:5}, | |
{price:20, rank:4}, | |
{price:9, rank:1} | |
] | |
var res = arr.sort(function(a,b){ | |
return a.price != b.price ? a.price - b.price : b.rank - a.rank; | |
}) | |
console.log(arr);</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [ | |
{price:18, rank:5}, | |
{price:8, rank:4}, | |
{price:12, rank:2}, | |
{price:12, rank:4}, | |
{price:8, rank:3}, | |
{price:15, rank:5}, | |
{price:20, rank:4}, | |
{price:9, rank:1} | |
] | |
var res = arr.sort(function(a,b){ | |
return a.price != b.price ? a.price - b.price : b.rank - a.rank; | |
}) | |
console.log(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment