数据的异常排序 - 按固定数据 id 排序

Unusual sorting of data - order by fixed data id

我遇到了一个非常不寻常的排序问题。我必须按照预定义的位置排列项目,就像我现在在数据库中的位置一样:

sid - auto increment
name - just something to distinguish varchar 100
sorting - this is where we define the exact position of the item 

如果我们给任何产品sorting = 2,它无论如何都会排在第二位。但是应该按照排序的定义。

我无法使用 order by sorting desc 排列项目,因为大多数项目都没有分配任何位置。由于部分已赋值的条目sorting = 0,按照计划,所有排序值不为零的条目都应该放在其他条目之前。

这是当前代码:

 <?php 
  $conn=mysqli_connect("localhost","root","","work");
   $getthedetails=mysqli_query($conn,"select sid,sorting,name from sorting order by sorting asc");
  while($thelistis=mysqli_fetch_array($getthedetails)){ ?>
  <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;">
    <?=$thelistis['sorting']?>
   </div>
   <?php } ?>

理想情况下,我希望排序类似于 1,0,0,4,5,0,0,8。

我想要的是this

试试下面这个

$getthedetails=mysqli_query($conn,"select id,sorting,name from tbl_sorting order by sorting asc");
$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 } ?>