我有如下所述的原始 csv 資料
James,Mary,Patricia,Anthony,Donald\n
145,10,100,39,101\n
21,212,313,28,1
在上面提到的字串中,列是用逗號,分隔的,第一行是column每個\n人的資料,然后是新行。我在這里想要實作的是它應該按如下所述進行排序。
Anthony,Donald,James,Mary,Patricia\n
39,101, 145,10,100\n
28,1,21,212,313
到目前為止,我嘗試的是,基于每個值\n的拆分,基于逗號,的進一步拆分,但在這種情況下,將沒有對排序值的正確參考。
我正在努力的部分
string data = "James,Mary,Patricia,Anthony,Donald\n145,10,100,39,101\n21,212,313,28,1";
var rows = data.Split('\n');
var unorderedNames = rows[0].Split(',');
基于 \n 拆分陣列

基于,逗號拆分名稱 -

現在,如果我實施排序,我相信我會丟失所有參考,因為名稱將被排序,但在第 2 行和第 3 行下方,提到的付款不會。
In my code mentioned above, The first line split the array into three based on \n. Then when I soft first line, I beleive I don't have reference of other values in the same array.
I appreciate if you can assist me to find some efficient method to convert this raw data into sored alphabetically with values in efficient way.
uj5u.com熱心網友回復:
public class StackDemo
{
private string source = "James,Mary,Patricia,Anthony,Donald\n145,10,100,39,101\n21,212,313,28,1";
public string ProcessString()
{
var rows = source.Split('\n');
var row1Values = rows[0].Split(',');
var row2Values = rows[1].Split(',');
var row3Values = rows[2].Split(',');
List<Person> people = new List<Person>();
for (int index = 0; index < 5; index )
{
people.Add(new Person()
{
Name = row1Values[index],
SomeValue = row2Values[index],
OtherValue = row3Values[index]
});
}
people.Sort((x, y) => x.Name.CompareTo(y.Name));
List<string> names = new List<string>();
List<string> someValues = new List<string>();
List<string> otherValues = new List<string>();
foreach (Person p in people)
{
names.Add(p.Name);
someValues.Add(p.SomeValue);
otherValues.Add(p.OtherValue);
}
string result = "";
result = BuildString(names, result);
result = BuildString(someValues, result);
result = BuildString(otherValues, result);
result = result.Remove(result.Length - 1, 1);
return result;
}
private static string BuildString(List<string> names, string result)
{
foreach (string s in names)
{
result = s ",";
}
result = result.Remove(result.Length - 1, 1);
result = "\n";
return result;
}
}
public class Person
{
public string Name { get; set; }
public string SomeValue { get; set; }
public string OtherValue { get; set; }
}
這段代碼非常基本,(粗魯)但它做了我認為你想要的?)
它還以與接收到的格式相同的格式回傳字串。
uj5u.com熱心網友回復:
我認為問題是您想將 CSV 的標頭排序為“隨便”的順序,并讓資料“隨它去”
想出一些方法來將您的資料表示為二維陣列:
var lines = File.ReadAlLines("path");
var data = lines.Skip(1).Select(line => line.Split(',')).ToArray(); //nasty way of parsing a CSV but it's accessory to this discussion..
var head = lines[0]
.Split(',')
.Select((s,i) => new { Name = s, Index = i })
.OrderBy(at => at.Name)
.ToArray();
head現在是已排序的標題,但它有一個附加屬性,可以告訴您哪個列data包含該人的資料。Anthony 是第一個heaD,但他們的 Index 是 3,所以我們應該從 Anthony 獲取資料data[3]
foreach(var person in head){
Console.WriteLine($"Now printing {person.name} data from column {person.Index}");
foreach(var line in data){
Console.Writeline(line[person.Index]);
}
}
我們沒有費心對資料進行排序(不這樣做更有效),我們只是將它所在的列存盤為已排序物件的一部分,然后不管人員排序順序如何,我們都通過該列訪問資料。排序head非常快,因為它只是幾個名字。它始終保持其“資料在哪里”的映射,因為Index無論資料的排序順序如何都不會改變head
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449943.html
