zoukankan      html  css  js  c++  java
  • php js 排序

     编写背景及排序 规则

      公司需要对游戏进行一系列的排序,在这里只说我自己遇到问题的哪一段

      //规则:$plat数据要根据$sort里的sort为相应 可以输入一个数字,即为该平台;

    解决思路:将$sort要固定单元先固定到$temp里面【占位置】,并将与$plat对应的单元删除

         用for循环依次将$plat还剩的单元插入$temp不为空的位置【填补空缺】

      

    php版

    $plat = [
        0 => [
            "code" => "9u",
            "order" => 101
        ],
    
        1 => [
            "code" => "xm",
            "order" => 107
        ],
    
        2 => [
            "code" => "gp",
            "order" => 115
        ],
    
        3 => [
            "code" => "dl",
            "order" => 120
        ],
    
        4 => [
            "code" => "baidu",
            "order" => 121
        ],
    
        5 => [
            "code" => "ow",
            "order" => 117
        ],
    
        6 => [
            "code" => "changxiang",
            "order" => 999
        ],
    
        7 => [
            "code" => "360",
            "order" => 1000
        ]
    ];
    
    $sort = [
        0 => [
            "code" => "ow",
            "sort" => 1
        ],
        1 => [
            "code" => "dl",
            "sort" => 6
        ],
    ];
    
    function formate_sort($sort)
    {
        $temp = [];
        foreach ($sort as $k => $v) {
            $temp[$v['code']] = $v['sort'];
        }
        return $temp;
    }
    function plat_sort($plat, $formate_sort)
    {
        $temp = [];
        $cou = count($plat);
        foreach ($plat as $k => $v) {
            if(isset($formate_sort[$v['code']])) {
                $temp[$formate_sort[$v['code']]] = $v;
                unset($plat[$k]);
            }
        }
    
        for ($i=0; $i< $cou; $i++) {
            if(!isset($temp[$i])) {
                $temp[$i] = array_shift($plat);
            }
        }
    
        $keys = array_keys($temp);
        sort($keys);
        $sort_plat = [];
        foreach ($keys as $k=>$v) {
            $sort_plat[$k] = $temp[$k];
        }
        return $sort_plat;
    }
    $formate_sort = formate_sort($sort);
    $plat_arr = plat_sort($plat, $formate_sort);

    js版

    // 数据 排序
    function traverseSortTable( plugin_sort_table,  sort_platform){
       
        var temp_table    = new Array();
        var temp_sort     = new Array();
        var temp_platform = new Array();
        var i = 0
       
        //先进行简单的排序并获取 相应的值
        $.each( plugin_sort_table, function( k, v ){
            i++; 
            temp_table.push({ 0 : i, 1 : v[1], 2 : v[2], 3:a_default( k, v )  });
        });
    
        //1、提取sort_platform里的code与sort生成  数组[baidu: 1, dl: 7] 
        $.each( sort_platform, function( k, v ){
            temp_sort[ v['code'] ] =v['sort'];
        });
    
        // 2、将满足翻入相应位子temp_platform,删除temp_table与之对应的coder单元
        var length = temp_table.length;
        var temp_temp_table = Array();
        $.each(temp_table, function(k, v){
            var temp = temp_sort[ v[2] ]; 
            if (temp >= 0) {
                v[3] = a_cancle( temp, v );
                temp_platform[temp] = v;
            } else {
                temp_temp_table.push(v);
            }
        });
        temp_table = temp_temp_table;
     
        // 3、将temp_table的code且不存在与temp_platform依次插入temp_platform 进行从小到大排序
        var index = 0;
        for (var i = 0; i < length; i++) {
            var temp = temp_platform[i];
            // console.log(typeof temp);
            if (temp === undefined) {
                 temp_platform[i] = temp_table[index];
                 index++;
            }
        }
     
        //4、对temp_platform
        // new_plugin_sort_table = $.unique( temp_platform ) ;   
        new_plugin_sort_table = temp_platform;   
        return false;
    }
  • 相关阅读:
    165. Compare Version Numbers
    164. Maximum Gap
    3、桶排序
    162. Find Peak Element
    160. Intersection of Two Linked Lists
    155. Min Stack
    154. Find Minimum in Rotated Sorted Array II
    153. Find Minimum in Rotated Sorted Array
    Linux/Unix系统编程手册 第二章:基本概念
    Linux/Unix系统编程手册 第一章:历史和标准
  • 原文地址:https://www.cnblogs.com/jxkshu/p/6249181.html
Copyright © 2011-2022 走看看