简体   繁体   English

用于按升序/降序对文本文件中的数据进行排序的算法

[英]Algorithms to sort data from text files in ascending/descending order C#

Right, let me try to explain what I am stuck on and what I am trying to do! 是的,让我尝试解释我的坚持和尝试做的事情! I have multiple .txt files, all with a large set of data inside them (one has days of the week, another with dates, and others with other data to do with stocks) which are all in descending order (so all the first piece of data of one .txt file matches the first piece of data in another .txt file). 我有多个.txt文件,所有文件中都包含大量数据(一个文件是一周中的某天,另一个文件是日期,而其他文件中的其他数据是与股票有关的),这些文件都是按降序排列的(因此第一部分都是.txt文件的数据匹配与另一个.txt文件中的第一数据。

I am trying to get my code to read all lines of the text files (using File.ReadAllLines), place that read data into a big array (if possible) so that if the user requests to see all data that is on a "Wednesday" or all data in the text files from 01/03/1999 - 31/03/1999 and the data to show on command line (I added a table - just run the code and you'll see what I mean) The user has to be able to Search by day or date and it needs to be able to be Sorted into ascending and descending order using an algorithm, In my head, I know what I need to do, but it is the implementation that is the struggle, I've tried Array.List(), Array.Sort(), Quicksort (wasn't useful at all) and more that I have forgotten after a good 3 hours worth of trial and error. 我正在尝试让我的代码读取文本文件的所有行(使用File.ReadAllLines),将读取的数据放入一个大数组中(如果可能的话),以便如果用户请求查看“星期三”中的所有数据”或从01/03/1999-31/03/1999的文本文件中的所有数据以及要在命令行上显示的数据(我添加了一个表-只需运行代码,您就会明白我的意思了)能够按日期或日期进行搜索,并且需要能够使用算法将其分类为升序和降序,在我的脑海中,我知道我需要做的事情,但是实现是最困难的尝试了Array.List(),Array.Sort(),Quicksort(根本没有用)以及经过3个小时的反复试验后我忘了的更多东西。

I'm still quite new to this, especially in terms of algorithms but hopefully I have explained it so it's at least understandable but open enough that it might help others. 我对此还是很陌生,尤其是在算法方面,但是希望我已经解释了它,因此它至少可以理解,但足够开放,可以对其他人有所帮助。 If it makes no sense, please ask questions and I'll answer them, I might have confused myself in writing this :P) Thanks in advance! 如果这没有任何意义,请提出问题,我会回答,我可能对写:P)感到困惑。

 <!-- Run this code to see the table --> <table style="width:100%"> <tr> <td>Date</td> <td>Day</td> <td>Open</td> <td>Close</td> <td>Difference</td> <td>Volume</td> </tr> <tr> <td>01/03/1999</td> <td>Monday</td> <td>312</td> <td>320</td> <td>...</td> <td>...</td> </tr> <tr> <td>10/03/1999</td> <td>Wednesday</td> <td>301</td> <td>289</td> <td>...</td> <td>...</td> </tr> <tr> <td>19/03/1999/</td> <td>Friday</td> <td>365</td> <td>342</td> <td>...</td> <td>...</td> </tr> </table> 

You do not need to write or implement any sorting algorithms to do this, it is a matter of parsing the data into object form. 不需要编写或实现任何排序算法来做到这一点,它是数据解析成对象形式的问题。

File.ReadAllLines simply dumps every line of the file into an array, and by itself will not be enough to organize your data into a meaningful way. File.ReadAllLines只是将文件的每一行都转储到数组中,仅靠其本身不足以将数据组织成有意义的方式。 You need to Parse the HTML to Deserialize the file into a list of objects. 您需要解析HTML以将文件反序列化为对象列表。

This will point you in the right direction in regard to parsing the HTML: What is the best way to parse html in C#? 这将为您指出解析HTML的正确方向: 在C#中解析html的最佳方法是什么?

You'll need to create a class with a property for each of your data fields. 您需要为每个数据字段创建一个带有属性的

After you have turned your files into objects, and have verified that the data is contained in the objects, you should have a List or an Array of these items. 在将文件变成对象并验证数据包含在对象中之后,您应该具有这些项目的列表或数组。 You can then use the LINQ extension method OrderBy to sort your data. 然后,您可以使用LINQ扩展方法OrderBy对数据进行排序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM