Search code examples
c#streamreaderstreamwriter

Sorting some records in the .txt file without disturbing other records and overwriting in the same file


I am trying to read a text file line by line and trying to sort ONLY "Puma" named shoes in ascending manner according to the price and save the changes in the same file without disturbing the order of "Nike" and "Adidas" shoes. It could be great if someone can help me in this. Thanks in advance.

Here is my code below which I tried

    public class Program
    {
      static string content;
      static string outputFile = @"C:\Users\Desktop\ShoeRack.txt";

      public static void Main(string[]args)
      {
        sorting();
      }
      public static void sorting()
      {
        try
        {
          string[] scores =System.IO.File.ReadAllLines(@"C:\Users\Desktop\ShoeRack.txt");

          var numbers = scores.OrderBy(x=>(x.Split(',')[2]));

          foreach(var dat in numbers)
         {
           content = dat.toString();
           writetoFile(outputFile,content);
         }

        }
        catch(Exception e)
        {
           Console.WriteLine(e);
           Console.ReadLine();
        } 
      }
      public static void writetoFile(string outputFile, string content)
      {
         using(System.IO.StreamWriter file = new System.IO.StreamWriter(@outputFile))
         {
            file.WriteLine(content);
         }
      }

    }

The Text File is below named ShoeRack.txt

ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,50,7,Red,Yes
Puma,55,6,Yellow,Yes
Puma,44,5,Red,Yes
Puma,45,4,Green,No

Output is

ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,50,7,Red,Yes
Puma,55,6,Yellow,Yes
Puma,44,5,Red,Yes
Puma,45,4,Green,No
ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,50,7,Red,Yes
Puma,55,6,Yellow,Yes
Puma,44,5,Red,Yes
Puma,45,4,Green,No 

Expected output is

ShoeCompany,Price,Size,Color,Avaibility
Nike,75,6,Red,Yes
Nike,80,5,Yellow,Yes
Nike,50,9,White,Yes
Nike,44,5,White,No
Adidas,50,7,Green,Yes
Adidas,55,8,Grey,Yes
Adidas,40,5,Red,Yes
Puma,45,4,Green,No
Puma,44,5,Red,Yes
Puma,55,6,Yellow,Yes
Puma,50,7,Red,Yes

Solution

  • What you code is not doing is checking to see if the string is Puma.

    static string content;
        static string outputFile = @"C:\Users\Desktop\ShoeRack.txt";
    
        public static void sorting()
        {
            try
            {
                string[] scores = System.IO.File.ReadAllLines(@"C:\Users\Desktop\ShoeRack.txt");
    
                //Split into two lists:  one that is Puma, one that is the others
                List<string> pumaEntries = new List<string>();
                List<string> otherEntries = new List<string>();
    
                foreach (string item in scores)
                {
                    if (item.Split(',')[0].ToUpper() == "PUMA")
                    {
                        pumaEntries.Add(item);
                    }
                    else
                    {
                        otherEntries.Add(item);
                    }
                }
                //Now sort the puma entries
                var sorted = pumaEntries.OrderBy(x => x.Split(',')[1]);
    
                //Now output the "Other" entries, then the Puma ones
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@outputFile))
                {
                    foreach (string dat in otherEntries)
                    {
                        file.WriteLine(dat);
                    }
                    foreach (string dat in sorted)
                    {
                        file.WriteLine(dat);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadLine();
            }
        }
    

    This code splits the Puma entries to a separate list for sorting, and keeps the others unchanged.

    Note that the string array is zero-based, to to sort by price, you need to sort by [1] not [2].