0

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.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272

1 Answers1

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();
Striter Alfa
  • 1,577
  • 1
  • 14
  • 31