Question: I have following HTML:

```
<div id="ServiceProvider" class="main_filter" name="filterDiv">
    <div class="filter_chkbox_div" style="display:none">
       <label class="ATT">AT&amp;T</label>
    </div>
    <div class="filter_chkbox_div" style="display:none">
       <label class="CABLEONE">Cable One</label>
    </div>
    <div class="filter_chkbox_div" style="display:none">          
       <label class="CABLEONEv6">Cable Onev6</label>
    </div>
    <div class="filter_chkbox_div" style="display:none">
        <label class="CHARTER">Charter</label>
    </div> 
</div>

```

At runtime I display only those labels that I receive in my array `providerCodes` 

**jQuery code**

```
for (var i = 0; i < providerCodes.length; i++) {
    var providerCheckBox = $("#" + providerCodes[i]);
    if (providerCheckBox.length > 0) {
        $(providerCheckBox).parent().show();
    }
}

```

Now I want to position these labels in accordance to the order I receive in `providerCodes`. For Example, if `providerCodes` array contain `CHARTER` on 0th index and `CABLEONE` on 1st index,I want to change the position of these labels accordingly. Currently, it will show `CABLEONE` first and then `CHARTER`. How can I do this in jQuery?

Answer: 
I would do this (provided that you have the div selector for the container):

```
var containerSelector = $("<container-selector>");
for (var i = 0; i < providerCodes.length; i++) {
   var providerCheckBox = $("#" + providerCodes[i]);
   if (providerCheckBox.length > 0) {
       $(providerCheckBox).parent().show().detach().appendTo(containerSelector);
   }
}

```
To reorder the elements we will get they index into the array and put them before this index of `.filter_chkbox_div`

```
var providerCodes = ["CABLEONEv6","CABLEONE"];
for (var i = 0; i < providerCodes.length; i++) {
  var providerCheckBox = $("." + providerCodes[i]);
  if (providerCheckBox.length > 0) {
    $('.filter_chkbox_div').eq(i).before(providerCheckBox.parent());
    providerCheckBox.parent().show();
  }
}

```

Demo : <https://jsfiddle.net/IA7medd/srkbcdox/>