Search code examples
javascriptjqueryfabricjsjquery-ui-sortable

Jquery Sortable Index Reverse


I'm using sortable with jQuery. When using Sortable it gives top-down index values. For example:

 <ul id="sortable">
        <li>Item 1</li> /O.Index
        <li>Item 2</li> /1.Index
        <li>Item 3</li> /2.Index
 </ul>

I want it to look like this:

<ul id="sortable">
            <li>Item 1</li> /2.Index
            <li>Item 2</li> /1.Index
            <li>Item 3</li> /0.Index
</ul>

How can I reverse this?

Thanks :)


Solution

  • You can append the new ordered children using .get() and Array.prototype.reverse():

    var ulEl = $('#sortable');
    ulEl.append(ulEl.children('li').get().reverse());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul id="sortable">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>