## **在个别业务中需要我们对数据进行排序** > 例如 我们需要给小说中的栏目进行排序 根据`order`字段 小的在最上面 而且order字段不能重复 不多说 下面就是代码 数据表如下 ![](https://img.kancloud.cn/dc/d9/dcd9ac48a1f3d311e76d4d089163d69a_252x272.png) ~~~ //排序 加入需要将 id=1的数据改成5 需要传值 id=1 order=5 public function order(Request $request) { $id = $request->id ?? 0; //需要修改的数据ID $order = $request->order ?? 0; //需要排序的大小 $old_order = DB::table("test")->where('id',$id)->value('order'); //判断数据是调大还是调小 ($order > $old_order 调大) 如果调大则 所有影响的数据 都要调小 if($order > $old_order){ $where = [['order','>',$old_order], ['order','<=',$order]]; $type = 1; }else{ $where = [['order','>=',$order], ['order','<',$old_order]]; $type = 2; } $update_data = $this->_executeOrder($where,$type); foreach ($update_data as $value){ DB::table("test")->where('id',$value->id)->update(['order' => $value->add_order]); } DB::table("test")->where('id',$id)->update(['order' => $order]); } //处理受影响的数据 public function _executeOrder($where,$type) { $update_data = DB::table("test")->where($where)->get(); foreach ($update_data as &$value){ if($type == 1){ $value->add_order = $value->order - 1; }else{ $value->add_order = $value->order + 1; } } return $update_data; } ~~~ ![](https://img.kancloud.cn/cd/2e/cd2e9608c4c78331c7f5f34bd94ca549_540x341.png) * 注意 以上代码为laravel框架 其他框架可根据情况做适当修改 ## 原生排序 ~~~ class MLQuickSort{ /** * Name 小值变大值 * Created by WML * Created on 2021/12/24 * @param array $old * @param array $new * @param array $arr * @return array */ static function minToMax($old=[],$new=[],$arr=[]) { $keep = []; foreach ($arr as $value){ if($value['id'] == $old['id']){ $keep[] = ['id' => $value['id'],'order' => $new['order']]; } if(($value['order'] <= $new['order']) && ($value['order']) >= $old['order']){ if($value['id'] == $old['id']) continue; $new_order = $value['order'] - 1; $keep[] = ['id' => $value['id'],'order' => $new_order]; }else{ $keep[] = $value; } } return $keep; } //大值变小值 static function maxToMin($old=[],$new=[],$arr=[]) { $keep = []; foreach ($arr as $value){ if($value['id'] == $old['id']){ $keep[] = ['id' => $value['id'],'order' => $new['order']]; } if(($value['order'] >= $new['order']) && ($value['order']) <= $old['order']){ if($value['id'] == $old['id']) continue; $new_order = $value['order'] + 1; $keep[] = ['id' => $value['id'],'order' => $new_order]; }else{ $keep[] = $value; } } return $keep; } } //$arr = [ // ['id' => 1,'order' => 1], // ['id' => 2,'order' => 2], // ['id' => 3,'order' => 3], // ['id' => 4,'order' => 4], // ['id' => 5,'order' => 5], //]; //return (new QuickSort)->minToMax(['id' => 1,'order' => 1],['id' => 1,'order' => 3],$arr); //结果 : //['id' => 1,'order' => 3] //['id' => 2,'order' => 1] //['id' => 3,'order' => 2] //['id' => 4,'order' => 4] //['id' => 5,'order' => 5] //return (new QuickSort)->maxToMin(['id' => 4,'order' => 4],['id' => 4,'order' => 2],$arr); //结果 : /*array(5) { [0]=> array(2) { ["id"]=> int(1) ["order"]=> int(1) } [1]=> array(2) { ["id"]=> int(2) ["order"]=> int(3) } [2]=> array(2) { ["id"]=> int(3) ["order"]=> int(4) } [3]=> array(2) { ["id"]=> int(4) ["order"]=> int(2) } [4]=> array(2) { ["id"]=> int(5) ["order"]=> int(5) } }*/ ~~~