I have just added a Javascript function in my html to generate random numbers between 30 and 100 for every list item, and I'm trying to figure out how to sort these numbers in reverse and make sure that the first item always have a value of 100.
To add context, my goal is to give percent matching scores to my search results.
ex: 90% Match
.
For example the expected result would be this:
List of item (in reverse order with 100 as default first value):
1. 100%
2. 92%
3. 86%
4. 80%
5. 71%
6. 65%
7. 58%
8. 45%
9. 30%
While at the moment it look like this:
List of item (random order):
1. 58%
2. 66%
3. 97%
4. 58%
5. 89%
6. 61%
7. 63%
8. 86%
9. 46%
The html where the numbers are currently being displayed:
<div>
{% if page_obj.object_list %}
<ol class="row top20" id="my_list">
{% for result in page_obj.object_list %}
<li id="list_item">
<div class="showcase col-sm-6 col-md-4">
<a href="{{ result.object.get_absolute_url }}">
<h3>{{result.object.title}}</h3>
<h5>{{ result.object.destination }}</h5>
<img src="{{ result.object.image }}" class="img-responsive">
</a>
</div>
</li>
{% endfor %}
</ol>
</div>
{% endif %}
How to sort the order in reverse and give a default value of 100 to the first result?
My script
var orderedList = document.getElementById("my_list");
var itemLength = 9; //REPLACE THIS WITH THE LENGTH OF THE ITEM LIST
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var listItem;
var randomInt;
for (var i = 0; i < itemLength; i++) {
listItem = document.getElementById("list_item");
randomInt = document.createTextNode(getRandomInt(30, 100).toString() + "%");
listItem.appendChild(randomInt);
orderedList.appendChild(listItem);
}
<ul id="my_list">
<li id="list_item"></li>
</ul>