This question is unique in that it requests a lookup to a json object and is not a simple array sort. I want to populate 4 input fields with the contents of splitString array. But I need to re-order the splitString array in descending (rsort) of the members[Rating]. members has 300+ entries, so it's counterproductive and unnecessary to sort it.
How can I do this? The code is my start...
HTML:
<button onclick="ratingSort()" title="Rating sort" tabindex="-1">
<img src="images/sort.png" alt= "sort by rating" /></button>
<input id = "I1" />
<input id = "I2" />
<input id = "I3" />
<input id = "I4" />
and a js file:
<script type="text/javascript">
function ratingSort() {
let dataString = "Williams, Bill|Reynolds, Beverly|Smith, Paul";
let splitString = dataString.split("|");
let sorted = [];
let finalResult;
var members = [
{ "Rating": "1500", "Name": "Williams, Bill"},
{ "Rating": "2000", "Name": "Smith, Paul" },
{ "Rating": "1000", "Name": "Jones, Jim" },
{ "Rating": "1750", "Name": "Reynolds, Beverly" } ]
// sort by value
members.sort(function (a, b) {
return a.Rating - b.Rating;
});
members.map(i=>{sorted.push(i.Name)});
finalResult = sorted.filter(function(i) {return
splitString.includes(i)});
console.log(finalResult);
for (let i = 0; i < finalResult.length; i++) {
$temp = finalResult[i];
if ($temp > "") {$("#I" + i).val($temp);
$("#L" + i).css('background-color', "#7E91F8");
}
}
}
</script>
The code doesn't work and generates inconsistent results.