我正在创建包含50行的文本文件,每行包含有关文件的信息。行中的单词用';'分隔。我需要按文件大小对冒泡排序算法排序文件,它是第三个字,并从控制台中排序的文件中写入行。这意味着第一行将具有最大的文件大小,第二行,第三行等......
到目前为止,我有这个:
Random rnd = new Random();
if (!File.Exists(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt")) //C:\Users\bartbo13it\Downloads\alpha.txt
{
string[] arrayx = new string[50];
string[] arrayy = arrayx.Select(ggg => String.Join(";", new string[] { @"C:\user\directory", "file" + rnd.Next(100, 999), rnd.Next(0, 999999).ToString(), "txt", rnd.Next(0, 1).ToString() })).ToArray();
File.WriteAllLines(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txtt", arrayy);
}
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt");
int pocet_radku = File.ReadAllLines(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt").Length;
List<int> velikostsouboru = new List<int>();
using (StreamReader sr = new StreamReader(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt"))
{
Console.WriteLine("Pocet radku " + pocet_radku);
for (int i = 0; i < pocet_radku; i++)
{
string radek = sr.ReadLine();
string[] rozdeleni = radek.Split(';');
int ParsePokus = Int32.Parse(rozdeleni[2]);
velikostsouboru.Add(ParsePokus);
}
}
int[] array = velikostsouboru.ToArray();
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - i - 1; j++)
{
if (array[j + 1] > array[j])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int a = 0; a < array.Length; a++)
{
Console.WriteLine(array[a] + " ");
}
Console.ReadKey();
它只是使用冒泡排序算法对文件大小进行排序,并在控制台中将文件大小从最大到最小排序。仍然需要编写整行排序,而不仅仅是文件大小。
我将不胜感激。
答案 0 :(得分:0)
// There are better ways to create a file, but I decided to keep it in the original.
const string filePath = @"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt";
var rnd = new Random();
if (!File.Exists(filePath))
{
var array = new string[50].Select(x => String.Join(";", new[]
{
@"C:\user\directory",
"file" + rnd.Next(100, 999),
rnd.Next(0, 999999).ToString(CultureInfo.InvariantCulture),
"txt",
rnd.Next(0, 1).ToString(CultureInfo.InvariantCulture)
})).ToArray();
File.WriteAllLines(filePath, array);
}
// It will read the file and fill a variable with the data
// Convert the value to int is necessary in the order process
// Otherside, it will consider 9 higher than 10.
var data = File.ReadAllLines(filePath)
.Select(value => value.Split(';'))
.Select(x => new
{
Folder = x[0],
FileName = x[1],
Size = Convert.ToInt32(x[2]),
Extension = x[3],
IsActive = x[4]
})
.ToList<dynamic>();
// You can change it using the bubble sort as you wish
// I preffer the linq way.
var orderedData = data.OrderByDescending(x => x.Size);
// Writes the header in console (du~)
Console.WriteLine("Folder" + "\t\t\t"
+ "FileName" + "\t"
+ "Size" + "\t"
+ "Extension" + "\t"
+ "IsActive");
foreach (var result in orderedData)
{
// If you want to write the result in a file, you do it here.
Console.WriteLine(result.Folder + "\t"
+ result.FileName + "\t\t"
+ result.Size + "\t"
+ result.Extension + "\t\t"
+ result.IsActive);
}
Console.ReadKey();