0

I have a container element containing div elements with a data-index value. I want to reorder the elements by the data-index, so they appear as 1, 2 and 3.

<div class="container">
    <div class="col-md-4" data-index="3">Col 3</div>
    <div class="col-md-4" data-index="1">Col 1</div>
    <div class="col-md-4" data-index="2">Col 2</div>
</div>

So how can I sort the elements by the data-index value?

Salman A
  • 262,204
  • 82
  • 430
  • 521
SteinTheRuler
  • 3,549
  • 4
  • 35
  • 71

5 Answers5

3

I am using flexbox to change the order of the divs:

let cols = Array.from(document.querySelectorAll(".container .col-md-4"));
cols.map((col)=>{
  let index = col.dataset.index;
  col.style.order = index;
})
.container{display:flex;flex-direction:column;}
<div class="container">
    <div class="col-md-4" data-index="3">Col 3</div>
    <div class="col-md-4" data-index="1">Col 1</div>
    <div class="col-md-4" data-index="2">Col 2</div>
</div>
enxaneta
  • 31,608
  • 5
  • 29
  • 42
2

Use document.querySelector to get all the children then use sort to sort this collection and iterate through this collection to append the element back to the div

function reorder() {
  let orderedString = '';
  [...document.querySelectorAll('.container div')].sort(function(a, b) {
    return a.dataset.index - b.dataset.index

  }).forEach(function(item) {
    document.querySelector('.container').appendChild(item)
  })

}
<div class="container">
  <div class="col-md-4" data-index="3">Col 3</div>
  <div class="col-md-4" data-index="1">Col 1</div>
  <div class="col-md-4" data-index="2">Col 2</div>
</div>
<button onclick="reorder()">Order</button>
brk
  • 48,835
  • 10
  • 56
  • 78
1

You can try with sort(), attr() and appendTo() like the following way:

$('.container div').sort(function(a,b) {
     a = Number($(a).attr('data-index'));
     b = Number($(b).attr('data-index'));
     return a - b;
}).appendTo('.container');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <div class="col-md-4" data-index="3">Col 3</div>
    <div class="col-md-4" data-index="1">Col 1</div>
    <div class="col-md-4" data-index="2">Col 2</div>
</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • 1
    I guess `sort()` should return `a - b` rather than `a > b`; it is -1, 0 or 1 depending on whether a is less than, equal or grater than b. – Damian Czapiewski Sep 16 '18 at 14:30
1

Use Array.sort function with proper callback:

$(".container div").sort(function(a, b) {
  return $(a).data("index") - $(b).data("index");
}).appendTo(".container");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  <div class="col-md-4" data-index="3">Col 3</div>
  <div class="col-md-4" data-index="1">Col 1</div>
  <div class="col-md-4" data-index="2">Col 2</div>
</div>
Salman A
  • 262,204
  • 82
  • 430
  • 521
0

Get the divs and sort it with sort() then attach to the parent doc with html()

var divList = $(".col-md-4");
divList.sort(function(a, b){
return $(a).data("data-index")-$(b).data("data-index")
});
$(".container").html(divList);
Osama
  • 2,912
  • 1
  • 12
  • 15