1

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

4
  • How did Apples get to the end? Commented Oct 29, 2014 at 5:54
  • You declared array as fruit and is applying sort on fruits. Commented Oct 29, 2014 at 5:57
  • 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 Commented Oct 29, 2014 at 5:57
  • Please update the question with your actual requirement. Otherwise there is a chance that we all waste some time. Commented Oct 29, 2014 at 5:59

2 Answers 2

1

You can use the

var fruit = ["Apples", "Oranges", "Pears", "Peaches"];
fruit.sort(function(a, b){
 return // Ur Logic
})

Please refer http://www.javascriptkit.com/javatutors/arraysort2.shtml

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please elaborate on your response with how to solve my specific issue?
You write your logic in the function, where you should return -1, if you want a to come before b in the sort, vice versa
Please go through the link I have provided, there u will have a detailed explanation on this. If you still have a doubt, feel free to ask
1

There are some ways to achieve what you want. Let's start defining some vars outside the click-function so it's not repeated with every new click:

var listOutput = document.getElementById('fruits'),
    select = document.getElementById('select'),
    opts = select.querySelectorAll('option'),
    len = opts.length,
    fruit = [];
// populate fruit with the values of all options
for (var i = 0; i < len; i++) fruit.push(opts[i].value);
fruit.sort(); // sort fruit alphabetically

If you have to support old browsers not knowing modern array methods:

select.addEventListener('click', function() {
    var text = '<ul>',
        idx = 0,
        sel = this.selectedOptions[0].value; // get the selected value
    for (; idx < len; idx++) if (fruit[idx] == sel) break; // get index of selected value
    for (var i = idx; i < len; i++) { // get all items from selected to end
        text += '<li>' + fruit[i] + '</li>';
    }
    for (var i = 0; i < idx; i++) { // get all items from first to selected
        text += '<li>' + fruit[i] + '</li>';
    }
    listOutput.innerHTML = text + '<ul';
});

Instead of two for-loops to print the items you can do it with one:

    // get all items from selected to end, then from first to selected
    for (var i = idx; i < len;) {
        text += '<li>' + fruit[i] + '</li>';
        if (++i == len && len != idx) i = 0, len = idx;
    }

Find a DEMO here. With modern array methods (find them here) it's less to write:

var listOutput = document.getElementById('fruits'),
    select = document.getElementById('select'),
    opts = select.querySelectorAll('option'),
    fruit = [].map.call(opts, function(opt) {return opt.value}).sort();

select.addEventListener('click', function() {
    var idx = fruit.indexOf(this.selectedOptions[0].value),
        arr = fruit.slice(idx).concat(fruit.slice(0, idx));
    listOutput.innerHTML = arr.reduce(function(a, b) {
        return a + '<li>' + b + '</li>'
    }, '<ul>') + '</ul>';
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.