Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a a recompilation of my 3 favorite answers on this board:</p> <ul> <li>jOk's best and simplest answer.</li> <li>Terry Porter's easy jQuery method.</li> <li>SmokeyPHP's configurable function.</li> </ul> <p>The results are an easy to use, and easily configurable function. </p> <p>First argument can be a select object, the ID of a select object, or an array with at least 2 dimensions.</p> <p>Second argument is optional. Defaults to sorting by option text, index 0. Can be passed any other index so sort on that. Can be passed 1, or the text "value", to sort by value. </p> <h2>Sort by text examples (all would sort by text):</h2> <pre><code> sortSelect('select_object_id'); sortSelect('select_object_id', 0); sortSelect(selectObject); sortSelect(selectObject, 0); </code></pre> <h2>Sort by value (all would sort by value):</h2> <pre><code> sortSelect('select_object_id', 'value'); sortSelect('select_object_id', 1); sortSelect(selectObject, 1); </code></pre> <h2>Sort any array by another index:</h2> <pre><code>var myArray = [ ['ignored0', 'ignored1', 'Z-sortme2'], ['ignored0', 'ignored1', 'A-sortme2'], ['ignored0', 'ignored1', 'C-sortme2'], ]; sortSelect(myArray,2); </code></pre> <p>This last one will sort the array by index-2, the sortme's.</p> <h1>Main sort function</h1> <pre><code>function sortSelect(selElem, sortVal) { // Checks for an object or string. Uses string as ID. switch(typeof selElem) { case "string": selElem = document.getElementById(selElem); break; case "object": if(selElem==null) return false; break; default: return false; } // Builds the options list. var tmpAry = new Array(); for (var i=0;i&lt;selElem.options.length;i++) { tmpAry[i] = new Array(); tmpAry[i][0] = selElem.options[i].text; tmpAry[i][1] = selElem.options[i].value; } // allows sortVal to be optional, defaults to text. switch(sortVal) { case "value": // sort by value sortVal = 1; break; default: // sort by text sortVal = 0; } tmpAry.sort(function(a, b) { return a[sortVal] == b[sortVal] ? 0 : a[sortVal] &lt; b[sortVal] ? -1 : 1; }); // removes all options from the select. while (selElem.options.length &gt; 0) { selElem.options[0] = null; } // recreates all options with the new order. for (var i=0;i&lt;tmpAry.length;i++) { var op = new Option(tmpAry[i][0], tmpAry[i][1]); selElem.options[i] = op; } return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload