需要将这样的string
数组排序为特殊格式。我们的数组是:
input1 = new string[12]{"Active1","12","mm","Active2","17","mm","Width","25","mil","Height","20","mil"}
我们想要的排序列表是:
sort = new string[6]{"Serial","Width","Height","Active1","Active2","Time"}
我输出的有效格式为:
输出= [{Serial,null,null},{Width,25,mil},{Height,20,mil},{Active1,12,mm},{Active2,17,mm},{Time,null ,null}]
有必要为null
数组中不存在的数据设置Input
值。
我将此代码用于我的目的:
var Output = (from i in Enumerable.Range(0, input.Length / 3)
let index = Array.IndexOf(sort, input[i * 3])
where index >= 0
select ne3w string[] { input[i * 3], input[i * 3 + 1] , input[i * 3 + 2]})
.OrderBy(a => Array.IndexOf(sort, a[0])).ToArray();
但它没有显示input
数组中不存在的值。
答案 0 :(得分:4)
我会把它放到一个单独的方法中:
private static IEnumerable<string[]> TransformInput(string[] input)
{
return from key in new[] { "Serial", "Width", "Height", "Active1", "Active2", "Time" }
let keyIndex = Array.IndexOf(input, key)
let hasData = keyIndex > 1
select new[]
{
key,
hasData ? input[keyIndex + 1] : null,
hasData ? input[keyIndex + 2] : null
};
}
然后按如下方式使用它:
var input1 = new string[12]
{ "Active1", "12", "mm", "Active2", "17", "mm", "Width", "25", "mil", "Height", "20", "mil" };
var sorted = TransformInput(input1);
答案 1 :(得分:1)
您可以使用以下方法执行此操作:
private string[][] Sort(string[] input)
{
List<string> inputList = new List<string> ();
inputList = input.ToList<string> ();
List<string[]> sortedList = new List<string[]> ();
string[] sort = new string[]{"Serial", "Width", "Height", "Active1", "Active2", "Time"};
foreach(string key in sort)
{
if (inputList.Contains<string> (key)) {
int i = inputList.IndexOf (key);
string[] t = new string[]{inputList[i],inputList[i+1],inputList[i+2]};
sortedList.Add (t);
}
else
{
string[] t = new string[]{key,null, null};
sortedList.Add (t);
}
}
return sortedList.ToArray<string[]> ();
}
希望它可以帮到你!
答案 2 :(得分:1)
在Nitesh上构建,但无需使用字典重复扫描input
using System.Linq; //at top of file
private static string[][] TransformInput(string[] input)
{
var sortOrder = new[] { "Serial", "Width", "Height", "Active1", "Active2", "Time" };
//dictionary pointing words to position in input
var inputDict = Enumerable.Range(0, input.Length/3)
.Select(i => i*3).ToDictionary(i => input[i]);
//Try to read position from dictionary; return nulls if fail
return sortOrder.Select(x => {
int i;
return (inputDict.TryGetValue(x, out i))
? new[]{x, input[i+1], input[i+2]}
: new[]{x, null, null};
}).ToArray();
}
答案 3 :(得分:1)
给出两组输入数据:
var input1 = new string[12]
{
"Active1","12","mm",
"Active2","17","mm",
"Width","25","mil",
"Height","20","mil"
};
var sort = new string[6]
{
"Serial","Width","Height","Active1","Active2","Time"
};
这对我有用:
var lookup =
input1
.Select((x, n) => new { x, n })
.ToLookup(xn => xn.n / 3)
.ToLookup(
z => z.ElementAt(0).x,
z => z.Skip(1).Select(w => w.x));
var result =
sort
.Select(x =>
new [] { x }
.Concat(lookup[x].SelectMany(z => z))
.Concat(new string[] { null, null })
.Take(3)
.ToArray())
.ToArray();
我得到了这个结果:
答案 4 :(得分:1)
我认为这段代码对您的问题有好处。
static List<string[]> SortedList(string[] input)
{
var sort = new string[6] { "Serial", "Width", "Height", "Active1", "Active2", "Time" };
List<string[]> output = new List<string[]>();
for (int i = 0; i < sort.Length; i++)
{
var findIndex = input.ToList().IndexOf(sort[i]);
if (findIndex != -1)
output.Add(new string[3]
{
input[findIndex],
input[findIndex + 1],
input[findIndex + 2]
});
else
output.Add(new string[3]
{
sort[i],
null,
null
});
}
return output;
}
现在你打电话给那个方法:
var input = new string[12] { "Active1", "12", "mm", "Active2", "17", "mm", "Width", "25", "mil", "Height", "20", "mil" };
var output = SortedList(input);