Last active
November 11, 2020 05:29
-
-
Save dayAlone/9669a85722ade443cdff84955c68695d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/xanuvon
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
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script> | |
<script src="http://yui.yahooapis.com/3.10.0/build/yui/yui.js"></script> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
var proposals = [{ price: 1234, weight: 0.9999984294495, id: 1 }, { price: 1231, weight: 0.9999984294495, id: 2 }, { price: 1231, weight: 0.9999984294496, id: 3 }]; | |
var result_lodash = []; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = proposals[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
proposal = _step.value; | |
var insert_to = _.sortedLastIndexBy(result_lodash, proposal, function (p) { | |
return p.price - p.weight / 1000; | |
}); | |
result_lodash.splice(insert_to, 0, proposal); | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator["return"]) { | |
_iterator["return"](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
var getIDs = function getIDs(list) { | |
return list.map(function (el) { | |
return el.id; | |
}); | |
}; | |
var result_map = proposals.sort(function (a, b) { | |
return a.price - b.price || b.weight - a.weight; | |
}); | |
console.log(getIDs(result_lodash)); | |
console.log(getIDs(result_map)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">let proposals = [ | |
{price: 1234, weight: 0.9999984294495, id: 1}, | |
{price: 1231, weight: 0.9999984294495, id: 2}, | |
{price: 1231, weight: 0.9999984294496, id: 3} | |
] | |
let result_lodash = [] | |
for(proposal of proposals) { | |
const insert_to = _.sortedLastIndexBy(result_lodash, proposal, (p) => ( | |
p.price - (p.weight / 1000) | |
)) | |
result_lodash.splice(insert_to, 0, proposal) | |
} | |
const getIDs = (list) => list.map(el => el.id) | |
const result_map = proposals.sort((a, b) => a.price - b.price || b.weight - a.weight) | |
console.log(getIDs(result_lodash)) | |
console.log(getIDs(result_map))</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
"use strict"; | |
var proposals = [{ price: 1234, weight: 0.9999984294495, id: 1 }, { price: 1231, weight: 0.9999984294495, id: 2 }, { price: 1231, weight: 0.9999984294496, id: 3 }]; | |
var result_lodash = []; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = proposals[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
proposal = _step.value; | |
var insert_to = _.sortedLastIndexBy(result_lodash, proposal, function (p) { | |
return p.price - p.weight / 1000; | |
}); | |
result_lodash.splice(insert_to, 0, proposal); | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator["return"]) { | |
_iterator["return"](); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
var getIDs = function getIDs(list) { | |
return list.map(function (el) { | |
return el.id; | |
}); | |
}; | |
var result_map = proposals.sort(function (a, b) { | |
return a.price - b.price || b.weight - a.weight; | |
}); | |
console.log(getIDs(result_lodash)); | |
console.log(getIDs(result_map)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment