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?
- sort element array sort()
- 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
Post a Comment