Sort div array based on alphabets during appending Jquery <p>I am stuck in a place where I have to sort the list of names when moved those list on button click.</p> <pre><code>sort() </code></pre> <p>method works fine but clicking on the same button again creating duplicates!, which is not I need.</p> <p>Is there any way I can fix this. used below code too(which took from stackoverflow) even this is not working.</p> <pre><code>function SortByName(a, b) { var aName = a.name.toLowerCase(); var bName = b.name.toLowerCase(); return ((aName &lt; bName) ? -1 : ((aName &gt; bName) ? 1 : 0)); } var selectArr = selectedList.sort(SortByName); </code></pre> <p><strong>MY CODE</strong></p> <pre><code> $(document).on('click', '#buttonAdd', function (e) { var divCount = $('#SelectedEmployeeList').find('div').length; if (divCount === 0) { selectedList = []; // Clearing the list, to add fresh employee list. } $('#EmployeeList :selected').each(function (i, selected) { selectedList[$(selected).val()] = $(selected).text(); }); // Returning DOM node from collection. var empListNode = $("#SelectedEmployeeList")[0]; console.log(empListNode); while (empListNode.firstChild) { empListNode.removeChild(empListNode.firstChild); } for (var empID in selectedList) { $("#SelectedEmployeeList").append("&lt;div class= EmpList id=" + empID + "&gt;" + selectedList[empID] + " &lt;span class='close'&gt;&amp;times;&lt;/Span&gt; &lt;/div&gt;"); } }); </code></pre> <p>This is the place where all sort should happen <code>#SelectedEmployeeList</code>.</p> <p>have shared the pics for references.</p> <p>Sort is happening correctly on the left like here :</p> <p><a href="https://i.stack.imgur.com/xK8Fk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xK8Fk.jpg" alt="enter image description here"></a></p> <p>after clicking on "Add All >> " items are moved but not sorted :</p> <p><a href="https://i.stack.imgur.com/00Azk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/00Azk.jpg" alt="enter image description here"></a></p> <p>any help will be appreciated.</p> <p>UPDATE 1 :</p> <p><a href="https://i.stack.imgur.com/zwezj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zwezj.jpg" alt="enter image description here"></a></p>