c# - 如何正确排序此数组?

标签 c# arrays sorting

我正在尝试对堆叠的立方体数组进行排序。 数组看起来像这样(一个例子)

Nth Cube | Position (0, its on the desk, >0,  its on another cube)
---
1|6
---
2|8
---
3|0
---
4|0
---
5|0
---
6|0
---
7|1
---
8|4
---

看起来像这样可视化

[7] [2]
---
[1] [8]
---
[6] [4] [3] [5]
---

我整理了另一个立方体:

string[,] t 在这种情况下是整个数组

static string[,] stackedCubes(string[,] t)
    {
        string[,] stackedHelp = new string[NumberOfStacked(),2];
        int j = 0;
        for (int i = 0; i < t.GetLength(0); i++)
        {
            if (t[i, 1]!="0")
            {

                stackedHelp[j, 0] = t[i, 0];
                stackedHelp[j, 1] = t[i, 1];
                j++;
            }
        }
        return stackedHelp;
    }

结果为:

1|6
---
2|8
---
7|1
---
8|4
---

现在我试图把它们从顶部放下(所以 7/2 然后 1/8)

string[,] t 从这里开始就是 stackedCubes

static string[,] Putdown(string[,] t)
    {
        string[,] stackedOrder = new string[NumberOfStacked(), 2];
        int j = 0;
        do
        {
            for (int i = 0; i < t.GetLength(0); i++)
            {
                if (t[i, 1] == t[j, 0])
                {
                    j = i;
                }
                else if(IsSomethingOnTheCube(t[j,0],t)==false)
                {
                    stackedOrder[i, 0] = t[j, 0];
                    stackedOrder[i, 1] = "0";
                    t[i, 1] = "0";
                }
            }
        }
        while (AreAllTheCubesOnTable(t) != true);

        return stackedOrder;
    }

这里我从第一个立方体 1(t[j,0]) 开始检查上面是否有东西,它在 t[i,1] 上找到它并返回 7(t[j,0] ) 然后回去。 我在这里检查立方体上是否有东西

    static bool IsSomethingOnTheCube(string Cube,string[,] t)
    {
        for (int i = 0; i < t.GetLength(0); i++)
        {
            if(Cube==t[i,1])
            {
                return true;
            }
        }
        return false;
    }

我还检查他们是否都在 table 上(用 do-while)

    static bool AreAllTheCubesOnTable(string[,] t)
    {
        for (int i = 0; i < t.GetLength(0); i++)
        {
            if (t[i, 1] != "0")
            {
                return false;
            }
        }
        return true;
    }

输出都是错误的:

7|0
---
7|0
---
7|0
---
7|0
---

什么时候应该是这样的:

7|0
---
1|0
---
2|0
---
8|0
---

我在哪里索引错误?(或其他)

最佳答案

在提供的样本中

string[] cubes = new string[]
  {"1|6", "2|8", "3|0", "4|0", "5|0", "6|0", "7|1", "8|4"};

问题的底线看起来像是随机的。让我们订购 最低的立方体(比如,按字典顺序),因此可视化将是:

lines:
   2:        [2]     [7]
   1:        [8]     [1]
   0:    [3] [4] [5] [6]  <- bottom line is sorted
------------------------
files:    0   1   2   3

完成后,我们可以实现两个帮助方法:

   // Line: 0 for lowest cubes
   private static int CubeLine(string value, IEnumerable<String> stack) {
      for (int line = 0; ; ++line) {
        string parent = value.Substring(value.IndexOf('|') + 1);

        if ("0".Equals(parent))
          return line;

        value = stack.First(item => item.StartsWith(parent + "|"));
      }
    }

    // File: 0 for leftmost cubes
    private static int CubeFile(string value, IEnumerable<String> stack) {
      string root = value;

      while (true) {
        string parent = root.Substring(root.IndexOf('|') + 1);

        if ("0".Equals(parent))
          break;

        root = stack.First(item => item.StartsWith(parent + "|"));
      }

      return stack
        .Where(item => item.Substring(value.IndexOf('|') + 1) == "0")
        .OrderBy(item => item)
        .Select((item, index) => new {
          item = item,
          index = index
        })
        .First(item => item.item == root)
        .index;
    }

然后您可以轻松地对任何您喜欢的内容进行排序。例如,让我们排序 最上面的立方体在前,如果最左边在前:

 string[] cubes = new string[]
   {"1|6", "2|8", "3|0", "4|0", "5|0", "6|0", "7|1", "8|4"};

  var result = cubes
    .Select (cube => new {
      name = cube.Substring(0, cube.IndexOf('|')),
      file = CubeFile(cube, cubes),
      line = CubeLine(cube, cubes) })
    .OrderByDescending(cube => cube.line)
    .ThenBy(cube => cube.file)
    .Select(cube => cube.name);

  Console.Write(string.Join(", ", result));

结果是

  2, 7, 8, 1, 3, 4, 5, 6

编辑:如果您想先从左到右排序,从上到下排序:

  var result = cubes
    .Select (cube => new {
      name = cube.Substring(0, cube.IndexOf('|')),
      file = CubeFile(cube, cubes),
      line = CubeLine(cube, cubes) })
    .OrderBy(cube => cube.file)
    .ThenByDescending(cube => cube.line)
    .Select(cube => cube.name);  

结果是

 3, 2, 8, 4, 5, 7, 1, 6

编辑 2:放置顺序:底部在前,随意放置

  var result = cubes
    .Select (cube => new {
      name = cube.Substring(0, cube.IndexOf('|')),
      file = CubeFile(cube, cubes),
      line = CubeLine(cube, cubes) })
    .OrderBy(cube => cube.line)
    .Select(cube => cube.name);  

结果:

  3, 4, 5, 6, 1, 8, 2, 7 

关于c# - 如何正确排序此数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060873/

相关文章:

c# - C# Ping(或 PingReply)是否采用平均 RTT?

c# - WCF 服务中的 Prism 模块系统?

c# - 在单个 SQLCLR 上下文连接中运行 1000-10000 个命令是否更好?

php - 在php中从json数组获取数据

python - 在 Python 的 NumPy 中创建数组时方括号和括号之间的区别

sorting - boost 弹性聚集结果

c# - 使用c#开发这样的winform

c - strlen(&string[]) 是如何工作的?

vba - Excel VBA : sorting a table featuring cells with drop-down lists

C - 对文本文件中的单词列表进行排序