在不干扰其他记录的情况下对 .txt 文件中的某些记录进行排序并覆盖同一文件

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

我正在尝试逐行读取文本文件并尝试根据价格以升序方式仅对 "Puma" 命名的鞋子进行排序,并将更改保存在同一个文件中而不打乱 [=] 的顺序31=] 和 "Adidas" 鞋子。如果有人可以帮助我,那就太好了。提前致谢。

这是我在下面尝试的代码

    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);
         }
      }

    }

文本文件的名称如下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

输出为

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 

预期输出为

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

使用字符串生成器构建您的内容,然后将该字符串添加到文件中。

 string[] scores =System.IO.File.ReadAllLines(@"C:\Users\Desktop\ShoeRack.txt");
                var content=new StringBuilder();  
              var numbers = scores.OrderBy(x=>(x.Split(',')[2]));

              foreach(var dat in numbers)
             {
               content = dat.toString();
               content.Append(content+Environment.NewLine);
             }
             writetoFile(outputFile,content.ToString());

最后使用

覆盖现有数据
System.IO.File.WriteAllText (@outputFile, contents);

您的代码没有做的是检查字符串是否为 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();
        }
    }

此代码将 Puma 条目拆分为单独的列表进行排序,并保持其他不变。

注意字符串数组是从零开始的,要按价格排序,需要按[1]而不是[2]排序。