using System.Linq;publicclassKata{
publicstaticstringSortCsvColumns(string csvFileContent){
var lines = csvFileContent.Split('\n').Select(a => a.Split(';')).ToArray();for(int i=0;i<lines[0].Length;i++){
for(int j=0;j<lines[0].Length-1;j++){
if(lines[0][j].CompareTo(lines[0][j+1])>0){
for(int k=0;k<lines.Length;k++){
var temp = lines[k][j];
lines[k][j]= lines[k][j+1];
lines[k][j+1]= temp;}}}}returnstring.Join("\n", lines.Select(a =>string.Join(";", a)));}}
答案2:
using System.Linq;publicclassKata{
publicstaticstringSortCsvColumns(string csvFileContent){
var lines = csvFileContent.Split('\n').Select(line => line.Split(';')).ToArray();var indices = lines[0].Select((x,i)=>new{
Value = x, Index = i }).OrderBy(x => x.Value).Select(x => x.Index).ToArray();returnstring.Join("\n", lines.Select(line =>string.Join(";", indices.Select(i => line[i]))));}}
答案3:
using System.Linq;using System;publicclassKata{
publicstaticstringSortCsvColumns(string csvFileContent){
var d = csvFileContent
.Split('\n').Take(1).SelectMany(row => row.Split(';')).Select((word, index)=> Tuple.Create(word, index)).OrderBy(t => t.Item1).Select((t,index)=> Tuple.Create(t.Item1, t.Item2, index)).ToDictionary(t => t.Item3, t=>t.Item2);returnstring.Join("\n",
csvFileContent
.Split('\n').Select