First I'm a very Beginner in c#, and Sorry for my english I'm trying to sort myfile:"D:\Test.txt" by the number sfter the first "|"
FROM:
AVDT353|180
|||14/01/2021||
GSDF445|10
|||14/01/2021||
MLKLMK6|17023
|||14/01/2021||
TO:
GSDF445|10
|||14/01/2021||
AVDT353|180
|||14/01/2021||
MLKLMK6|17023
|||14/01/2021||
First, load the text file. Then, create a list of strings by using line breaks as the delimiter. Finally, sort the lines using Linq, splitting after the |character. Finally, write the strings to file, putting each on a new line.
string contents = File.ReadAllText(@"D:\Test.tx");
var lines = contents.Split(Environment.NewLine).ToList();
var sorted = lines.OrderBy(l => l.Split("|")[1]).ToList();
// To update the content of the file:
File.WriteAllText(@"D:\Out.tx", string.Join(Environment.NewLine, sorted));
void SortFile()
{
DirectoryInfo d = new DirectoryInfo(@"D:\Test");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files
string str = "";
foreach (FileInfo file in Files)
{
int fileNo = Convert.ToInt32(file.Name.Split('|')[1]);
for (int i = 0; i < Files.Length; i++)
{
if (fileNo < Convert.ToInt32((Files[i].Name.Split('|')[1])))
{
//Add it to list or something
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.