C# 使用Array.Sort仅对元素进行排序,而不对索引号进行排序?

C# 使用Array.Sort仅对元素进行排序,而不对索引号进行排序?,c#,arrays,sorting,unity3d,indexof,C#,Arrays,Sorting,Unity3d,Indexof,首先是我的代码的快速运行 这里我有两个数组,一个游戏对象和一个浮点数组 public class PlaceManager : MonoBehaviour { private GameObject[] playerList; private float[] distanceValue; 开始时我调用FindAllPlayers函数,更新时我调用两个函数 void Start() { FindAllPlayers(); } void Update() { FindPlayerXV

首先是我的代码的快速运行

这里我有两个数组,一个游戏对象和一个浮点数组

public class PlaceManager : MonoBehaviour
{
private GameObject[] playerList;
private float[] distanceValue;
开始时我调用FindAllPlayers函数,更新时我调用两个函数

void Start()
{
    FindAllPlayers();
}
void Update()
{
    FindPlayerXValues();
    DeterminePlace();
}
FindAllPlayers的功能是查找带有标记播放器的所有对象,然后将索引号分配给播放器。对于多人游戏,它将由播放器槽排序,如player1、player2等

public void FindAllPlayers()
{
    if (playerList == null)
    {
        playerList = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < playerList.Length; i++)
        {
            playerList[i].GetComponent<CharacterStats>().playerNumber = i;
        }
    }
}
determinateplace函数首先对距离值数组进行排序。接下来,它更新位置

我的计划是,它从链接的playerList数组元素中获取myPosition变量,然后指定链接的distanceValue元素在排序后的位置的索引号

    public void DeterminePlace()
    {
        Array.Sort(distanceValue);
        for (int i = 0; i < distanceValue.Length; i++)
        {

            playerList[i].GetComponent<CharacterStats>().myPosition = Array.IndexOf(distanceValue, distanceValue[i]); 
        }
    }
}
这似乎就是现实

[0]=distanceValue[0] = 1st Place --> [3]=distanceValue[3] = 4th Place
[1]=distanceValue[1] = 2nd Place --> [0]=distanceValue[0] = 1st Place
[2]=distanceValue[2] = 3rd Place --> [1]=distanceValue[1] = 2nd Place
[3]=distanceValue[3] = 4th Place --> [2]=distanceValue[2] = 3rd Place
[4]=distanceValue[4] = 5th Place --> [4]=distanceValue[4] = 5th Place
我可以在代码中实现什么来获得更接近第一个结果的东西


提前感谢您的帮助

我怀疑问题出在这一行:

playerList[i].GetComponent<CharacterStats>().myPosition = Array.IndexOf(distanceValue, distanceValue[i]);

对于每个玩家,应该在排序后的距离数组中找到他们的X位置,从而找到他们的排名。

如果我理解正确,您可以先让distanceValue[n]对应于玩家列表[n],然后对distanceValue进行排序,这会打破配对?您可能需要使用吗?这将使用第一个数组中的值进行排序,第二个数组的排序方式与第一个数组的排序方式相同!我所需要做的就是颠倒顺序,上面说第一名的球员是第四名。非常感谢你!
[0]=distanceValue[0] = 1st Place --> [3]=distanceValue[3] = 4th Place
[1]=distanceValue[1] = 2nd Place --> [0]=distanceValue[0] = 1st Place
[2]=distanceValue[2] = 3rd Place --> [1]=distanceValue[1] = 2nd Place
[3]=distanceValue[3] = 4th Place --> [2]=distanceValue[2] = 3rd Place
[4]=distanceValue[4] = 5th Place --> [4]=distanceValue[4] = 5th Place
playerList[i].GetComponent<CharacterStats>().myPosition = Array.IndexOf(distanceValue, distanceValue[i]);
playerList[i].GetComponent<CharacterStats>().myPosition = Array.IndexOf(distanceValue, playerList[i].transform.position.x * -1);