I used this to sort a gallery of images where the sort array would be altered by an ajax call. Hopefully it can be useful to someone.
var myArray = ['2', '3', '1'];_x000D_
var elArray = [];_x000D_
_x000D_
$('.imgs').each(function() {_x000D_
elArray[$(this).data('image-id')] = $(this);_x000D_
});_x000D_
_x000D_
$.each(myArray,function(index,value){_x000D_
$('#container').append(elArray[value]); _x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>_x000D_
<div id='container'>_x000D_
<div class="imgs" data-image-id='1'>1</div>_x000D_
<div class="imgs" data-image-id='2'>2</div>_x000D_
<div class="imgs" data-image-id='3'>3</div>_x000D_
</div>
_x000D_
Fiddle: http://jsfiddle.net/ruys9ksg/