javascript - Sort DIVs alphabetically without destroying and recreating them? -


i'd able sort bunch of div elements on page click of button.

all of them have simple text content, example:

<div class='sortme'>     ccc </div> <div class='sortme'>     aaa </div> <div class='sortme'>     bbb </div> <div class='sortme'>     ddd </div> 

when user clicks button, should rearranged in alphabetical order. there's number of ways this, can contents array, sort array , recreate html based on that.

this example of such solution:

http://jsfiddle.net/hibbard_eu/c2heg/

however, wouldn't play nice lot of code being used, , hoping able move divs around without doing destructive. possible?

  1. sort element array sort()
  2. reattach(in sorted state) appendto()

$('.sortme').sort(function(a, b) {    if (a.textcontent < b.textcontent) {      return -1;    } else {      return 1;    }  }).appendto('body');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  <div class='sortme'>    ccc  </div>  <div class='sortme'>    aaa  </div>  <div class='sortme'>    bbb  </div>  <div class='sortme'>    ddd  </div>


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -