I would like to sort an array alphabetically starting with an option in a select. Here's my code:
HTML
<select id="select">
<option>Apples</option>
<option>Oranges</option>
<option>Peaches</option>
<option>Pears</option>
</select>
<div id="fruits">
<ul>
<li>Apples</li>
<li>Peaches</li>
<li>Pears</li>
<li>Oranges</li>
</ul>
</div>
Javascript
document.getElementById('select').addEventListener('click', function() {
var index;
var fruit = ["Apples", "Oranges", "Pears", "Peaches"];
var listOutput = document.getElementById('fruits');
var text = "<ul>";
fruit.sort();
for (index = 0; index < fruit.length; index++) {
text += "<li>" + fruit[index] + "</li>";
}
text += "</ul>";
listOutput.innerHTML = text;
});
I've been able to sort the fruit alphabetically, but I'd like to be able to sort alphabetically based on what is selected. For example, if I picked Oranges in the select I'd like the fruits to be output as so:
Oranges
Peaches
Pears
Apples
I'd like to loop the array starting at the selected option alphabetically and once all the strings have been looped, start at the top of the alphabet and continue until all the strings have been output
fruitand is applying sort onfruits.