title: Find duplicate LI class names and group them in a UL using jQuery
body: <p>I have a list of items,</p>
  
  <pre><code>&lt;ul class="testList"&gt;
    &lt;li class="a"&gt;Should be grouped in a UL with id name = a&lt;/li&gt;
    &lt;li class="b"&gt;2&lt;/li&gt;
    &lt;li class="c"&gt;Should be grouped in a UL with id name = c&lt;/li&gt;
    &lt;li class="c"&gt;Should be grouped in a UL with id name = c&lt;/li&gt;
    &lt;li class="d"&gt;2&lt;/li&gt;
    &lt;li class="e"&gt;3&lt;/li&gt;
    &lt;li class="a"&gt;Should be grouped in a UL with id name = a&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I am trying to locate duplicate LI classes and then group them into ULs.</p>
  
  <p>Here is my jQuery code</p>
  
  <pre><code>$('ul.testList li').each(function(index, el) {
      var li = $(this);
      var cls = li.attr('class');
      var match = $('li[class="' + cls + '"]');
  
      if (match.length &gt; 0) {
          var ul = $('&lt;ul id="'+ cls +'"&gt;&lt;/ul&gt;');
          match.appendTo(ul);
  
          $(this).add(match).addClass('matched');
      }
  });
  </code></pre>
  
  <p>See the <a href="http://jsfiddle.net/" rel="nofollow">jsFiddle</a>: <a href="https://jsfiddle.net/b4vFn/41/" rel="nofollow">https://jsfiddle.net/b4vFn/41/</a></p>