[英]C# array sorting by %
I have array with letters, like this:我有字母数组,如下所示:
10% A
20% A
30% A
40% B
50% B
60% B
70% B
80% C
90% C
100% D
I need code to give me result like this:我需要代码来给我这样的结果:
A 10%-30%
B 40%-70$
C 80%-90%
D 100%-100%
If your array is sorted like that, this should work for you:如果你的数组是这样排序的,这应该适合你:
var result = values.GroupBy(x => x.Last())
.Select(
x =>
string.Format("{0} {1} - {2}", x.Key.ToString(),
x.First().Split()[0],
x.Last().Split()[0]));
From my point of view, topic starter was mean something like this:从我的角度来看,主题启动器的意思是这样的:
static void Main() {
var array = new [] { "A", "A", "A", "B", "B", "B", "B", "C", "C", "D" };
var result = new Collection < string > ();
//as u call it "resolution":
const int resolution = 10;
var currentSymbol = array[0];
var startIndex = 1;
for (var index = 0; index < array.Length; index++) {
if (currentSymbol != array[index]) {
result.Add(currentSymbol + " " + startIndex * resolution + "%-" + index * resolution);
currentSymbol = array[index];
startIndex = index + 1;
}
if (index + 1 == array.Length) {
result.Add(currentSymbol + " " + startIndex * resolution + "%-" + (index + 1) * resolution);
currentSymbol = array[index];
startIndex = index + 1;
}
}
foreach(var a in result) {
Console.WriteLine(a);
}
Console.ReadLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.