简体   繁体   中英

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).

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.

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!

 <!-- 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. You need to Parse the HTML to Deserialize the file into a list of objects.

This will point you in the right direction in regard to parsing the HTML: What is the best way to parse html in C#?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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