Home:ALL Converter>sorting and fixing a issue array php sort issue forced sorting

sorting and fixing a issue array php sort issue forced sorting

Ask Time:2015-10-19T18:37:13         Author:Amani

Json Formatter

Hello i am having an unusuall issue in my code sorting problem please see the below code

    <?php 
       $conn=mysqli_connect("localhost","root","","work");
       $getthedetails=mysqli_query($conn,"select sid,sorting,name from sorting   order by sid desc");
       $arrayList = array();
       while($thelistis=mysqli_fetch_array($getthedetails)){
         $arrayList[] = $thelistis;
       }

      foreach($arrayList as $key=>$list){
      if($list['sorting'] != 0){
        //Keep current element of array
        $temp = $arrayList[$key];
        //Make current element same as with the one to swap
        $arrayList[$key] = $arrayList[$list['sorting']-1];
        //Swap the kept one to its place
        $arrayList[$list['sorting']-1] = $temp;
      }
     }


      foreach($arrayList as $key=>$list){?>

    <div style="width:100px;font-weight:bold;color:#000;font-size:19px;font-    family:arial;height:100px;padding:49px;background:silver;float:left;margin:10px;  ">
      <?php echo $list['sorting']?>
      < /div>
       <?php } ?>

what ia mhaving the issue here is that i am not getiing the last sid first if the sorting value is zero. what i mean is that i want to arrange the items by descending sid order and if the sorting value of the item is not zero then it should go to its sorting position like

it hould arrange this by

sid9,sid8,(if sorting is 3 for sid 5)sort3,sid4,sid6,sort2 .......

the above code gives me something like sid9,sid8,sort3,sid6,sort2 .....

what i want is that place all the items with sorting value first and then arrange the vacant places by items in sid descending order like item with sid 20 (if 20 is the total no of items) will come first if sorting value of any item is not 1 and if there is any item with sorting value =1 and that is not item with sid 20 then item with sid 20 will come to second position

Author:Amani,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/33212283/sorting-and-fixing-a-issue-array-php-sort-issue-forced-sorting
yy