简体   繁体   English

如何使用c#按字母顺序对文本文件进行排序

[英]How to sort a text file in alphabetical order using c#

it prints all the names from last name to fist in the text file but i am stuck trying to print it in alphabetical order by last name它打印文本文件中从姓氏到拳头的所有名称,但我试图按姓氏的字母顺序打印它

here is the code i have so far:这是我到目前为止的代码:

namespace cse1302_Lecture18_FileIO_Prez
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("NameInput.txt");  //if file in bin/debug

            char[] delims = {','};

            //string[] names = {"",""};

            while(!sr.EndOfStream)
            {
                string data_line = sr.ReadLine();
                //names = data_line.Split(delims);
                Console.WriteLine(data_line);

            }
            sr.Close();
        }
    }

}

Try using LINQ.尝试使用 LINQ。 I've assumed here that you file contains two fields per line delimited with a comma, where the first is the First Name and the second the Surname.我在这里假设您的文件每行包含两个字段,用逗号分隔,其中第一个是名字,第二个是姓氏。

var lines = System.IO.File.ReadLines("NameInput.txt");
var linesOrderedBySurname = lines.OrderBy((p) => p.Split(',')[1]).ToList();

First store the names into a list , then sort them based on last name like:首先将名称存储到list ,然后根据last name对它们进行排序,例如:

var names = new List<string>();
while(!sr.EndOfStream)
{
   names.Add(sr.ReadLine());
}

names.Sort((x, y) => x.Split(',')[1].CompareTo(y.Split(',')[1]));

Then just display list items in the console.然后只在控制台中显示列表项。

foreach(var name in names)
    Console.WriteLine(name);

This assumes that last name comes after first name and they are delimited with a comma .这假设姓氏在名字之后,并且它们用逗号分隔。

Try to use File.ReadAllLines instead of StreamReader you need to load all lines of the file and then use LINQ to order them尝试使用File.ReadAllLines而不是StreamReader您需要加载文件的所有行,然后使用LINQ对它们进行排序

 static void Main(string[] args)
            {
                var lastNames = File.ReadAllLines("yourtxtfile.txt").OrderBy(line => line.Split(',')[1]);    
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何将数据网格中的列按字母顺序排序? - C# How do I sort the columns in a datagrid into alphabetical order? 如何使用 c# 对文本文件进行排序 - How to sort a text file using c# 如何按工作日的顺序排序(如在日历周中)而不是字母顺序(在C#中)? - How to sort by order of the weekdays (as in a calendar week) instead of alphabetical order (in C#)? 如何使用C#以升序对富文本框进行排序 - How to sort rich text box in ascending order using c# 如何根据其中一个按字母顺序对 4 个单独的字符串数组(具有相同的长度)进行排序? (在 C# 中) - How can I sort 4 seperate string arrays (with the same lenght), according to one of them in alphabetical order? (in C#) C#按字母顺序从xml文件到文本文件对单词进行排序 - C# Sorting words from xml file to text file in alphabetical order C#如何检查项目列表是否按字母顺序排列 - c# how to check if list of Items are in Alphabetical order or not 如何在C#中按字母顺序打印字符串中出现的字符? - How to print occurrence of a character in string in alphabetical order in C#? 按值对字典排序 - 按字母C#降序 - Sort dictionary by value - descending THEN alphabetical C# C#按照字母和长度排序Arraylist字符串 - C# sort Arraylist strings alphabetical and on length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM