<div id="source">
<div id="d1"></div>
<div id="d2"></div>
<div id="d3" data-rel="d1"></div>
<div id="d4" data-rel="d1"></div>
<div id="d5" data-rel="d2"></div>
<div id="d6" data-rel="d3"></div>
<div id="d7" data-rel="d3"></div>
<div id="d8" data-rel="d3"></div>
<div id="d9" data-rel="d5"></div>
<div id="d10" data-rel="d5"></div>
<div id="d11" data-rel="d5"></div>
<div id="d12" data-rel="d10"></div>
<div id="d13" data-rel="d10"></div>
<div id="d14" data-rel="d10"></div>
<div id="d15" data-rel="d13"></div>
</div>
<div id="destination"></div>
var root = $('#destination');
$('#source div').each(function(index) {
var dataRel = $(this).data('rel');
var itemId = $(this).attr('id').replace('d', '');
var container = root;
if(dataRel) {
container = $('#u' + dataRel.replace('d', ''));
}
container.append('<ul id="u'+ itemId +'"><li>' + itemId + '</li></ul>');
});
https://jsfiddle.net/TheDeadOne/vsxarq8v/