[英]how can sort alphabetically a text file?
我正在嘗試對文本文件進行排序
像這樣的文字
word1,word2,word3
word1,word2,word3
word1,word2,word3
。
。
。
word5,word6,word3
我需要排序,但是此代碼無法正常工作
using System;
using System.IO;
class myclass
{
static void Main()
{
string infile = File.ReadAllText(@"C:\Users\diego\Desktop\finalregex.txt");
string outfile = File.ReadAllText(@"C:\Users\diego\Desktop\finalregex.txt");
var contents = File.ReadAllLines(infile);
Array.Sort(contents);
File.WriteAllLines(outfile, contents);
}
}
請幫我
假定示例中的格式,就足夠了:
// File paths.
const string inFile = "in.txt";
const string outFile = "out.txt";
// Read file.
var inContents = File.ReadAllText(inFile);
// Organize contents.
var contentsArray = inContents.Replace(Environment.NewLine, ",")
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// Sort contents.
var sortedContents = contentsArray.OrderBy(c => c);
// Write file.
File.WriteAllText(outFile, string.Join(",", sortedContents));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.