0

I have a list here I want to short according to DESC order. like 10,9,8,7,6,5,4,3,2,1 I am targetting data-id to change. but here it's starting from lowest value. I want to start from upper value

jQuery('.photos').sortable({
    stop: function(event, ui){
        let myIncrementor = GetLowestDataIdValue(); //Custom value returned from function
        $(".ui-sortable li").each(function(i, el){
               $(el).attr('data-id',myIncrementor);
               myIncrementor += 1; //increment it
        });
    }
});

function GetLowestDataIdValue(ui){
    let lowestVal = -1;
    $('.photos.ui-sortable').find('li').each(function(i, el){
         if(parseInt($(el).attr('data-id')) < lowestVal || lowestVal === -1){
              // alert(lowestVal);
               lowestVal = parseInt($(el).attr('data-id'));
         }
    });

    return lowestVal;
}
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<ul class="photos ui-sortable">
    <li class="photo" data-id="21">pawal</li>
    <li class="photo" data-id="22">sigma</li>
    <li class="photo" data-id="23">notea</li>
    <li class="photo" data-id="24">missouri</li>
    <li class="photo" data-id="25">Ooops</li>
    <li class="photo" data-id="26">Ratee</li>
</ul>

jsfiddle

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
  • Possible duplicate of [jQuery sort elements using data id](https://stackoverflow.com/questions/13490391/jquery-sort-elements-using-data-id) – barbsan Nov 16 '18 at 14:33
  • @barbsan my question is not duplicate. I already did that you marked. I want just DESC order not start from 1 – Lemon Kazi Nov 16 '18 at 15:43

2 Answers2

1

You can sort in descending order using solution from this question, just change order of elements in callback (subtract a.dataset.id from b.dataset.id)

$('.ui-sortable li').sort(function(a,b) {
     return parseInt(b.dataset.id) - parseInt(a.dataset.id);
}).appendTo('.photos');
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<ul class="photos ui-sortable">
    <li class="photo" data-id="21">pawal</li>
    <li class="photo" data-id="22">sigma</li>
    <li class="photo" data-id="23">notea</li>
    <li class="photo" data-id="24">missouri</li>
    <li class="photo" data-id="25">Ooops</li>
    <li class="photo" data-id="26">Ratee</li>
</ul>
barbsan
  • 3,418
  • 11
  • 21
  • 28
0

I solved my problem. Here I used

myIncrementor -= 1;

Solved code is here

jQuery('.photos').sortable({
    stop: function(event, ui){
        let myIncrementor = GetLowestDataIdValue(); //Custom value returned from function
        $(".ui-sortable li").each(function(i, el){
               $(el).attr('data-id',myIncrementor);
               myIncrementor -= 1; //increment it
        });
    }
});

function GetLowestDataIdValue(ui){
    let lowestVal = +1;
    $('.photos.ui-sortable').find('li').each(function(i, el){
         if(parseInt($(el).attr('data-id')) > lowestVal || lowestVal === +1){
              // alert(lowestVal);
               lowestVal = parseInt($(el).attr('data-id'));
         }
    });

    return lowestVal;
}
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
<ul class="photos ui-sortable">
<li class="photo" data-id="26">Ratee</li>
<li class="photo" data-id="25">Ooops</li>
 <li class="photo" data-id="24">missouri</li>
 <li class="photo" data-id="23">notea</li>
  <li class="photo" data-id="22">sigma</li>
    <li class="photo" data-id="21">pawal</li>
   
    
   
    
    
</ul>
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67