首页 > 解决方案 > String 和 Int 数组排序

问题描述

我在排序数组时遇到问题。我目前正在尝试优化我玩的策略游戏中的一个东西,为此我需要计算我的联盟所有成员之间的距离,第一个到其他成员的距离等等。实际上这样做没问题。但是现在,我要做的是对距离数组“升序”进行排序,问题是,我需要编写相应的昵称来匹配距离。我一直在寻找 2 天,但我无法找到一个可行的解决方案。

我试图在排序之前复制数组,但我需要未排序的数组,并且使用该排序函数,它也对副本进行排序!实际上提供的代码很好,说到距离精度,但没有按升序排序。如果我对距离进行排序,昵称不再对应。我不知道为什么它们以 pseudo_list 的顺序出现,因为它应该通过 nSort2() 排序这是我到目前为止得到的结果:

//Sorting Distance[i] Array List
function nSort(arr) 
{
  return arr.sort((a, b) => a - b);
}

//Calculating Distance
function calcDist(xA, yA, xB, yB)
{
    return Math.sqrt(Math.pow((xB-xA), 2)+Math.pow((yB-yA), 2));
}

 //Here i'm trying to retrieved unsorted position of distance by index to sort the nicknames by their respective distances
function nSort2(arr_str, arr_nbr)
{
    var arr_nbr2 = arr_nbr.splice(0);
    var arr_sort = nSort(arr_nbr2);
    var str_sort = [];

    arr_str.forEach(function(element, i) 
    {
        j = arr_sort.indexOf(arr_nbr2[i], i);
        str_sort[i] = arr_str[j];
    });
    console.log(str_sort);
    return str_sort;
}

var pseudo_list = ["teddy95", "gabrielc", "ngozi"]; //The list (I just put the first 3 to not to write to much unnecessary code)
var x_ = [29, 26, 4]; // The X Coordinate list
var y_ = [519, 461, 143]; // The Y Coordinate list
var distance = [[]]; // The 2D Array for distance (distance[0][0] being the member's distance tower himself (which is obviously 0).

//Calculating Distances And Storing them in the 2D Array
y_.forEach(function(element, i) 
{
    distance[i] = [];
    x_.forEach(function(element, j) 
    {
        distance[i][j] = Math.ceil(calcDist(x_[i], y_[i], x_[j], y_[j]));
    });
});

//Displaying Sorted Array ascending (Trying)
y_.forEach(function(element, i) 
{
    x_.forEach(function(element, j) 
    {
            document.write(pseudo_list[i] + ' -> ' + nSort2(pseudo_list, distance[i])[j] + ': ' + distance[i][j] + '<br>');
    });
});

标签: javascript

解决方案


我认为你的问题来自于过度复杂的数据结构(我不是在侮辱你只是分享一个观点)。

在下面的代码中,所有输入(伪、x、y)都存储在一个对象中,因此玩家数据更易于操作。然后我没有使用矩阵,因为您最终会创建新问题,即我期望距离[1][2] = distance[2][1] 因此排序会产生重复的结果(并且对角线没有帮助,因为它代表与自己的距离)。相反,我有一个没有重复的一维数组,即它包含从第一个元素到所有其他元素的距离(即第二个,第三个,...),然后是“右边的元素”中的第二个元素(即第三个元素) , 第四, ...), ... 一旦你掌握了所有的距离信息,排序是一项微不足道的任务,因此显示结果也是如此。

        //Calculating Distance
        function calcDist(xA, yA, xB, yB) {
            return Math.sqrt(Math.pow((xB - xA), 2) + Math.pow((yB - yA), 2));
        }

        let players = [{
            pseudo: "teddy95",
            x: 29,
            y: 519
        },
        {
            pseudo: "gabrielc",
            x: 26,
            y: 461
        },
        {
            pseudo: "ngozi",
            x: 4,
            y: 143
        }]

        let distances = []

        players.forEach(function (element, i) {
            for (let j = i + 1; j < players.length; ++j) {
                distances.push({
                    player1: element,
                    player2: players[j],
                    distance: Math.ceil(calcDist(element.x, element.y, players[j].x, players[j].y))
                })
            }
        })

        distances.sort(function (a, b) { return a.distance - b.distance })

        distances.forEach(function (element, i) {
            document.write(element.player1.pseudo + ' - ' + element.player2.pseudo + ' dist ' + element.distance + '<br>')
        })


推荐阅读