I am creating text file with 50 rows, each row have information about files. Words in row are separated by ';'. I need to sort files with bubble-sort algorithm by file size, it is third word, and write rows from file sorted in console. That means first row will have biggest file size, second, third, etc...
So far I have this:
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();
It just sorts file size with bubble-sort algorithm and writes file sizes in console sorted from biggest to smallest. Still need to write whole lines sorted, not just file size.
I will appreciate any help.