0

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>
  • I'd use SQLite and insert all the data into a table, then return that table using Select queries and whatever sorting you want. Is that not an option? – Ron Beyer Apr 23 '15 at 00:38
  • Your description of how the files relate to each other is hard to follow. I don't understand what "so all the first piece of data of one .txt file matches the first piece of data in another .txt file" means - how can that be true if one has days of the week and another has dates? Snippets of the files would be helpful. Also, you should almost certainly be importing this into a database rather than attempting to work with flat files. – ChrisV Apr 23 '15 at 00:39
  • @ChrisV Hi, so for example you have the day of the week (say Thursday) and then in another text file you have the date (so 23/04/2015). Database might have to be the option, I am being rather ambitious :P Cheers for the help :) – Adam Endean Apr 23 '15 at 00:43
  • 1
    Tenuously linking data by having it in the same ordinal row of various random text files is a Very Bad Idea. It's incredibly fragile. You will save yourself a lot of hurt in the long term if you learn SQL basics. – ChrisV Apr 23 '15 at 01:22
  • A `DataTable` will do everything you need: http://www.dotnetfunda.com/articles/show/131/datatable-adding-modifying-deleting-filtering-sorting-rows-readingwrit – Steve Wellens Apr 23 '15 at 01:27
  • This question is too broad. You need to try _something_. Do some research, try to figure out what the basic structure of your code and implementation details will be. If you have some _specific_ problem during that process, post that _specific_ question. – Peter Duniho Apr 23 '15 at 04:22
  • @AdamEndean did my answer help at all? – marknuzz May 12 '15 at 01:25
  • 1
    @Nuzzolilo Yes it did, sorry I forgot to thank you! – Adam Endean May 13 '15 at 10:56

1 Answers1

1

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.

Community
  • 1
  • 1
marknuzz
  • 2,847
  • 1
  • 26
  • 29