Skip to content

Instantly share code, notes, and snippets.

@Reedef
Forked from anonymous/index.html
Created September 21, 2016 00:08
Show Gist options
  • Save Reedef/de88f022df0e5ca589163a0a1f745836 to your computer and use it in GitHub Desktop.
Save Reedef/de88f022df0e5ca589163a0a1f745836 to your computer and use it in GitHub Desktop.
JS Bin [美团面试两列排序] // source https://jsbin.com/mejapis
<!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>
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