开发者

Sort CSV comparing only first element in each row

开发者 https://www.devze.com 2023-01-08 17:33 出处:网络
How do you sort comma separated values in a file by compa开发者_运维技巧ring only first element in each row?

How do you sort comma separated values in a file by compa开发者_运维技巧ring only first element in each row?


  • Read the file line by line
  • Split the lines at the commas
  • Sort
  • Write back to file


Something like:

var result = File.ReadAllLines(pathToFile).OrderBy(line => line.Split(",")[0])

Just note that this assumes your input is always valid. You would have to add your own error checking (empty rows, empty files, etc)

0

精彩评论

暂无评论...
验证码 换一张
取 消