【问题标题】:Unity C# and Array SortingUnity C# 和数组排序
【发布时间】:2017-11-25 13:23:24
【问题描述】:

所以我知道如何在 php 中对数组进行排序并保留键,但我在统一方面遇到了一些麻烦。

假设我有一个数组 itemcost = new int[10];

    itemcost[1] = 100;
    itemcost[2] = 300;
    itemcost[3] = 900;
    itemcost[4] = 300;
    itemcost[5] = 100;
    itemcost[6] = 300;

对这种降序排序的最佳方式是什么

提前致谢。

【问题讨论】:

  • 不要使用单独值的数组,使用包含所有相关值属性的类数组(或列表)。
  • 当然这是理想的,但问题是虽然这对我来说在 php 中很容易做到,但在 c# 中却证明要做到这一点要困难得多!
  • 说我有 def, att, cost, name 属性为每个项目存储它们的最佳方法是通过 att(降序)排序,然后说出列表中的第二个项目?跨度>
  • 检查我的更新答案
  • 顺便说一下,数组索引从零开始

标签: c# arrays sorting unity3d


【解决方案1】:

根据类属性或元素属性对类/结构的通用列表或数组进行排序

创建一个类来保存成本及其索引,然后对该类的数组或列表进行排序

您可以为类中的每个项目添加def、att、cost、name属性

这里是例子

public class CostData
    {
        public int Cost;
        public int ID;
        public CostData(int CostAmount, int CostID)
        {
            Cost = CostAmount;
            ID = CostID;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<CostData> itemcost = new List<CostData> {
                                                    new CostData(100, 1),
                                                    new CostData(300, 2),
                                                    new CostData(900, 3),
                                                    new CostData(300, 4),
                                                    new CostData(100, 5),
                                                    new CostData(300, 6)
                                                   };

        List<CostData> SortedList = itemcost.OrderByDescending((CostData i) => i.Cost).ToList();

        Console.WriteLine(SortedList[0].Cost.ToString() + " on index " + SortedList[0].ID.ToString());

        // you can do same with Arrray

        CostData[] itemcost2 = new CostData[] {
                                                    new CostData(100, 1),
                                                    new CostData(300, 2),
                                                    new CostData(900, 3),
                                                    new CostData(300, 4),
                                                    new CostData(100, 5),
                                                    new CostData(300, 6)
                                                   };

        CostData[] SortedList2 = itemcost2.OrderByDescending((CostData i) => i.Cost).ToArray();

        Console.WriteLine(SortedList2[0].Cost.ToString() + " on index " + SortedList2[0].ID.ToString());
    }

【讨论】:

  • 他需要保留数组的索引。
【解决方案2】:
var result = Enumerable.Range(0, itemcost.Length)
              .OrderByDescending(index => itemcost[index])
              .ToList();

result.ForEach(index => Console.WriteLine(index + " : " + itemcost[index]));

将返回按顺序排序的项目成本索引列表。

here

【讨论】:

    【解决方案3】:

    Array.Sort(Array keys, Array items)

    keysArray 中的每个键在 itemsArray 中都有对应的项。当一个键在排序过程中被重新定位时,itemsArray 中的对应项也会被类似地重新定位。因此itemsArray是按照keysArray中对应key的排列方式进行排序的。

    int[] indices = new int[itemcost.Length];
    
    for (int i = 0; i < itemcost.Length; i++)
    {
       indices[i] = i;
    }
    
    Array.Sort(itemcost, indices);
    

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多