3

I have a div that contains a set of links and my goal is to automatically sort those links by content, follow the examples below for better understanding:

before

<div id="sort-this-div">
<a href="http://something45yer.com">Content3</a>
<a href="http://somethingeyerty.com">Content1</a>
<a href="http://somethingfwegrw.com">Content2</a>
<a href="http://somethingt43rwer.com">Content4</a>
</div>

after

<div id="sort-this-div">
<a href="http://somethingeyerty.com">Content1</a>
<a href="http://somethingfwegrw.com">Content2</a>
<a href="http://something45yer.com">Content3</a>
<a href="http://somethingt43rwer.com">Content4</a>
</div>
demongolem
  • 9,474
  • 36
  • 90
  • 105
Shadow
  • 4,168
  • 5
  • 41
  • 72

1 Answers1

6

Assuming a jQuery-like environment:

sorted = $('#sort-this-div a').sort(function(a, b) {return a.innerHTML > b.innerHTML});
$('#sort-this-div').html('');
sorted.each(function(i, a) {$('#sort-this-div').append(a)});
Jesse Dhillon
  • 7,841
  • 1
  • 34
  • 34