接受基于优先级的数组行值以形成逗号分隔的字符串

时间:2017-06-16 01:39:37

标签: c# .net

我有一个锯齿状的数组

string[][] toMerge;

toMerge[0] = {"School", "Train", "Bag", "Choclate", " ", " "}; 
toMerge[1] = {"College", " ", " " , "chicken", " ", " "}; 
toMerge[2] = {"work", "car", " ", "Burger", " ", " "};

有另一个单维数组

string[] order = {"College", "Work", "School"};

我需要通过检查带有顺序数组的锯齿状数组的第一列并根据我需要选择列值并生成逗号分隔字符串的顺序来对锯齿状数组进行排序。

在上面的情况下,输出应为

String output = "College, Car, Bag, Chicken, , ";

有人可以用我能写的最短代码帮助我。

1 个答案:

答案 0 :(得分:0)

试试这个

            string[][] toMerge = {
                new string[] {"School", "Train", "Bag", "Choclate", " ", " "}, 
                new string[] {"College", " ", " " , "chicken", " ", " "},
                new string[] {"work", "car", " ", "Burger", " ", " "}
                                  };

            List<string> results = new List<string>();

            for (int i = 0; i < toMerge.First().Count(); i++)
            {
                string min = toMerge.Where(x => x[i].Trim().Length > 0).Select(x => x[i]).Min();
                results.Add(min == null ? " " : min);

            }

            Console.WriteLine(string.Join(",",results));
            Console.ReadLine();