HOW TO SORT MULTISELECTED ITEMS?

Tom Cruser

Total Post:28

Points:196
Posted by  Tom Cruser
 1140  View(s)
Ratings:
Rate this:
I have already finished the function of grouping the selected items and unselected items, but I need to sort them in alphabetic order meanwhile. My js code is shown below:

$("#id").multiselect({
        beforeopen: function (event, ui) {
            var selected = $("#id option:selected");
            selected.remove();
            $("#id").prepend(selected);
            $("#id").multiselect('refresh');
        }
    });

How can I achieve this?

  1. Barbara Jones

    Post:13

    Points:91
    Re: How to sort multiselected items?

    Here's a nice(-ish) way to do it:



    $("#clickity").click(function () {
            $("#derp>span").sort(function (a, b) {
                return (a.textContent ||a.innerText).localeCompare(b.textContent || b.innerText);
            }).prependTo("#derp");
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="derp"><span>g</span><span>o</span><span>t</span><span>h</span><span>s</span></div>
    <button id="clickity">Sort!</button>

      Modified On Apr-06-2018 02:05:42 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!