Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对锯齿数组进行排序_C# - Fatal编程技术网

C# 如何对锯齿数组进行排序

C# 如何对锯齿数组进行排序,c#,C#,我正在制定一个程序,使滚动计划自动化。我已经完成了大部分工作,但是我无法对输出进行排序,因为它是一个锯齿状数组。我需要将锯齿数组中每个数组的第二列从最高到最低排序 using System; using System.Linq; namespace Auto_Initiative { class Program { static void Main(string[] args) { string[] encounter =

我正在制定一个程序,使滚动计划自动化。我已经完成了大部分工作,但是我无法对输出进行排序,因为它是一个锯齿状数组。我需要将锯齿数组中每个数组的第二列从最高到最低排序

using System;
using System.Linq;

namespace Auto_Initiative
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] encounter =
            {
                "Wizard", "18", "-2",
                "Bard", "9", "3",
                "Goblin 1", "16", "1",
                "Goblin 2", "14", "1"
            };

            int[][] numbers = new int[encounter.Length / 3][];
            int loop = 0;
            for(int i = 0; i > encounter.Length; i += 3)
            {
                // Name number, real roll
                numbers[loop] = new int[2] {i, Int32.Parse(encounter[i + 1]) + Int32.Parse(encounter[i + 2])};
            }
            Console.ReadKey();
        }
    }
}

设计软件的一个部分是为计划如何使用它选择正确的数据结构。有时需要冗余数据,但我们不知道您需要什么来做出决定。正如谢尔盖所提到的,你应该考虑创建一个自定义类,我已经展示了下面的一个例子。还要注意的是,
string[]
实际上并不是一个锯齿状数组。根据定义,锯齿状数组具有大小可变的嵌套数组。上面描述的数据结构可以放在一个规则字符串[][]中,并且不会出现锯齿状

面向对象的应用 您要查找的内容存储在
unitsSortedBySecondColumn

class so65865986
{
    static void Main(string[] args)
    {
        Encounter encounter = new Encounter
        {
            Units = new List<EncounterUnit> { 
                new EncounterUnit{
                    Name = "Wizard",
                    Column1 = 18,
                    Column2 = -2,
                },
                new EncounterUnit{
                    Name = "Bard",
                    Column1 = 9,
                    Column2 = 3,
                },
                new EncounterUnit{
                    Name = "Goblin 1",
                    Column1 = 16,
                    Column2 = 1,
                },
                new EncounterUnit{
                    Name = "Goblin 2",
                    Column1 = 14,
                    Column2 = 1,
                },

            },
        };

        var unitsSortedBySecondColumn = encounter.Units
            .OrderBy(u => u.Column1)
            .Select(u => new int[] { u.Column1, u.Column2 })
            .ToArray();

    }


}

class EncounterUnit
{
    public string Name;
    public int Column1; //Change name to whatever it means
    public int Column2; //Change name to whatever it means
}

class Encounter
{
    public List<EncounterUnit> Units;
}
其他注释 还有,另一个注释。您不需要使用
Int32
,因为建议您使用提供的别名,在本例中为
int

使用以下代码:

string[] encounter =
{
   "Wizard", "18", "-2",
   "Bard", "9", "3",
   "Goblin 1", "16", "1",
   "Goblin 2", "14", "1"
};

int[,] numbers = new int[encounter.Length / 3, 3];
for (int i = 1; i < encounter.Length / 4; i++)
{
   for (int j = 0; j < encounter.Length / 3; j += 1)
   {
      numbers[j, i] = Convert.ToInt32(encounter[i + (j * 3)]);
      Console.Write(numbers[j, i] + "    ");
   }
   Console.WriteLine(Environment.NewLine);
}
Console.ReadLine();
string[]遭遇=
{
“向导”、“18”、“-2”,
“吟游诗人”、“9”、“3”,
“妖精1”、“16”、“1”,
“地精2”,“14”,“1”
};
整数[,]个数=新整数[contrace.Length/3,3];
for(inti=1;i
为什么在这里使用锯齿阵列?为什么要使用数组并将数字作为字符串呢。创建一个类来保存这三个字段,并使用一个类实例列表/数组。这不是一个锯齿状数组,这只是
string[]
,这让我在给出答案时感到厌烦。在这种情况下,锯齿状数组应该是
string[][]
或者
string[,]
。如果你坚持让它成为锯齿状数组,那么你应该让每一行都成为自己的
string[]
。这能回答你的问题吗?及及
string[] encounter =
{
   "Wizard", "18", "-2",
   "Bard", "9", "3",
   "Goblin 1", "16", "1",
   "Goblin 2", "14", "1"
};

int[,] numbers = new int[encounter.Length / 3, 3];
for (int i = 1; i < encounter.Length / 4; i++)
{
   for (int j = 0; j < encounter.Length / 3; j += 1)
   {
      numbers[j, i] = Convert.ToInt32(encounter[i + (j * 3)]);
      Console.Write(numbers[j, i] + "    ");
   }
   Console.WriteLine(Environment.NewLine);
}
Console.ReadLine();