-
-
Save Reedef/de88f022df0e5ca589163a0a1f745836 to your computer and use it in GitHub Desktop.
JS Bin [美团面试两列排序] // source https://jsbin.com/mejapis
This file contains 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 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