我正在使用C#阅读.txt文件,该文件包含单词列表,我需要按字母顺序对列表进行排序
static void Main(string[] args)
{
StreamReader objReader = new StreamReader(
@"C:\Users\thoma\Documents\Visual Studio 2019\Backup Files\data.txt");
string orden = "";
ArrayList arrText = new ArrayList();
while (orden != null)
{
orden = objReader.ReadLine();
if (orden != null) arrText.Add(orden);
}
objReader.Close();
foreach (string sOutput in arrText)
Console.WriteLine(sOutput);
Console.WriteLine("Order alphabetically descendant press 'a': ");
Console.WriteLine("Ordener ascending alphabetical press 'b': ");
orden = Console.ReadLine();
switch (orden)
{
case "a":
string ordenado = new String(orden.OrderBy(x => x).ToArray());
Console.WriteLine(ordenado);
break;
case "b":
Console.WriteLine("");
break;
}
Console.ReadLine();
}
这是我到目前为止的代码。 .txt文件显示它没有问题,但是在输入while语句并按选项时,它不会返回任何内容。
在arrText中存储.txt文件的单词,这些单词是:“在”,“在”,“在”时。
我需要在按下“ a”键时的while语句中,以字母顺序显示单词列表:“ are”“ in”“ while”。
答案 0 :(得分:3)
我会提供一个更好的分离和缩短版本:
var choices = new Dictionary<ConsoleKey, bool?>()
{
{ ConsoleKey.D1, true },
{ ConsoleKey.D2, false }
};
var ascending = (bool?)null;
while (ascending == null)
{
Console.WriteLine("Please choose between ascending and descending order.");
Console.WriteLine("Press 1 for ascending");
Console.WriteLine("Press 2 for descending");
var choice = Console.ReadKey(true);
ascending = choices.ContainsKey(choice.Key) ? choices[choice.Key] : null;
}
var lines = File.ReadAllLines("c:/data.txt");
lines = ascending.Value
? lines.OrderBy(x => x).ToArray()
: lines.OrderByDescending(x => x).ToArray();
foreach (var line in lines)
{
Console.WriteLine(line);
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
甚至是这样:
var choices = new Dictionary<ConsoleKey, Func<string[], string[]>>()
{
{ ConsoleKey.D1, xs => xs.OrderBy(x => x).ToArray() },
{ ConsoleKey.D2, xs => xs.OrderByDescending(x => x).ToArray() }
};
var ascending = (Func<string[], string[]>)null;
while (ascending == null)
{
Console.WriteLine("Please choose between ascending and descending order.");
Console.WriteLine("Press 1 for ascending");
Console.WriteLine("Press 2 for descending");
var choice = Console.ReadKey(true);
ascending = choices.ContainsKey(choice.Key) ? choices[choice.Key] : null;
}
var lines = ascending(File.ReadAllLines("c:/data.txt"));
foreach (var line in lines)
{
Console.WriteLine(line);
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
答案 1 :(得分:2)
这是我对你的问题的看法。注意,在循环文件中的文本之前,我正在询问排序顺序。
using System.Linq;
using System.IO;
class Program
{
static void Main(string[] args)
{
var lines = File.ReadAllLines("c:/data.txt");
var ascending = false;
var chosen = false;
do
{
Console.WriteLine("Please choose between ascending and descending order.");
Console.WriteLine("Press 1 for ascending");
Console.WriteLine("Press 2 for descending");
var choice = Console.ReadKey(true);
switch (choice.Key)
{
case ConsoleKey.D1:
ascending = true;
chosen = true;
break;
case ConsoleKey.D2:
ascending = false;
chosen = true;
break;
default:
Console.WriteLine("Invalid Choice");
break;
}
} while (!chosen);
var sequence = ascending
? lines.OrderBy(x => x)
: lines.OrderByDescending(x => x);
foreach (var line in sequence)
{
Console.WriteLine(line);
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
答案 2 :(得分:1)
这也应该在对提供的示例代码进行最小更改的情况下起作用。
首先,将ArrayList更改为List<string>
List<string> arrText = new List<string>();
第二,使用List OrderBy或OrderByDescending方法进行订购
string ordenado = string.Format("{0}{1}{0}", "'", string.Join("','", arrText.OrderBy(x => x)));
答案 3 :(得分:0)
使用ArrayList作为原始主题,List更加灵活,但是如果必须,则需要处理该类型的限制。
public static void Main()
{
StreamReader objReader = new StreamReader(@"C:\\Temp\\data.txt");
string orden = "";
ArrayList arrText = new ArrayList();
while (orden != null)
{
orden = objReader.ReadLine();
if (orden != null)
arrText.Add(orden);
}
objReader.Close();
foreach (string sOutput in arrText)
{ Console.WriteLine(sOutput); }
Console.WriteLine("Order alphabetically descendant press 'a': ");
Console.WriteLine("Ordener ascending alphabetical press 'b': ");
orden = Console.ReadLine();
switch (orden)
{
case "a":
arrText.Sort();
break;
case "b":
arrText.Sort();
arrText.Reverse();
break;
}
foreach (string sTemp in arrText)
{ Console.Write(sTemp); }
Console.WriteLine();
Console.ReadLine();
}