---
title: "How to sort multiselected items?"  
description: "How to sort multiselected items?"  
author: "Tom Cruser"  
published: 2014-10-31  
updated: 2014-10-31  
canonical: https://www.mindstick.com/forum/2477/how-to-sort-multiselected-items  
category: "jquery"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# How to sort multiselected items?

I have [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) finished the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) of grouping the [selected items](https://www.mindstick.com/forum/352/i-need-to-display-my-checkboxlist-selected-items-in-a-label-how-to-do) and unselected items, but I need to sort them in alphabetic [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) meanwhile. My js [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) 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?\

## Replies

### Reply by Barbara Jones

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>
```


---

Original Source: https://www.mindstick.com/forum/2477/how-to-sort-multiselected-items

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
