0

I have stored the following information in text file. I am using the file stream and stream reader object to read it. I got an array method:

string[] MyArray = allText.Split(new string[]{" ",Environment.NewLine},  
    StringSplitOptions.RemoveEmptyEntries);

This is to store the entire content of a text file by splitting in terms of words.

How do i do the same thing and make the following data get stored in a multidimensional arraylist. By sorting it.

Princeton       12/12/2013      04:13PM
Leon            10/11/2013      05:13PM
Shawn           09/09/2013      04:00PM
Sandy           07/09/2012      09:00AM
Grumpy          18/09/2013      10:00AM

Visualize it like in tables. All this data is stored in a multidimensional array list. Names have to be sorted and stored, date has to be sorted and stored and time has to be sorted and stored. Only the particular format in each given case is accepted.


This is what i tried but it keeps generating an error stating that the get and set methods have not been marked as extern or abstract and therefore must have a body.

using System; 
using System.Collections;
using System.IO; 
using System.Text; 
class Planner 
{    public string firstName { get; set;}    
     public string lastName { get; set; }    
     public DateTime dateTime { get; set; } 
}
class exe 
{    
     public static void Main(string[] args)    
     {
             List<Planner> t = new List<Planner>();
             StreamReader sr = new StreamReader("@cScheduler.txt")) 
             {
                        string line = string.Empty;
                        while ((line = sr.ReadLine()) != null)
                        {
                                string[] lines = line.Split(' ').ToArray();
                               //add to your list
                               t.Add(new Test() { firstName = lines[0], lastName = lines[1], dateTime = DateTime.ParseExact("MM/dd/yyyy hh:mm:ss tt",  
    lines[2] + lines[3], 
           CultureInfo.InvariantCulture) });
                         } 
              } //Your Ordered list now t = t.OrderBy(x => x.dateTime).ToList<Planner>();   

} }

3
  • Could you give an example of multidimensional-format table? I can't imagine it for this moment. Commented Jul 29, 2013 at 6:50
  • 1
    You might make your task a bit easier if you make a little class LineObject { string Name {get;set;} DateTime DateValue {get;set;} }, read all the lines into that structure and have a List<LineObject> to hold them. Then you can OrderBy(x => x.Name) or x => x.DateValue.Date or OrderBy(x => x.DateValue.Hour).ThenBy(x => x.DateValue.Minute). Commented Jul 29, 2013 at 6:51
  • The sample code you posted at the end has no references to the text example posted above it. First name? Last name? It seems you only have a name, and then a date and possibly a time. Commented Jul 29, 2013 at 8:27

1 Answer 1

1

I don't like multidimensional arrays - I'll use a List instead. It's got an array underneath anyway, so you can do as you please with it. ;)

List<string> lines = new List<string>();
foreach (var line in MyArray)
  lines.Add(new List<string>()(line.Split(' ')); //feel free to change the .Split as needed
lines = lines.OrderBy(l => l[0]).ThenBy(l => l[1]).ThenBy(l => l[2]).ToList(); //will sort by name, then by date, then by time

There, lines should contain separated and sorted data. Note that the sorting is based on alphabetical sorting as everything is still being treated as a string. You can parse the data in the Linq query if you need to use specific types.

Of course from there you can parse the data (into actual string, DateTime and possibly TimeSpan instances) via appropriate classes or whatever else you need.

Here's the code you posted, corrected to work with the sample data you provided:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    class Planner
    {
        public string firstName { get; set; }
        public DateTime dateTime { get; set; }
    }

    class exe
    {
        public static void Main(string[] args)
        {
            List<Planner> t = new List<Planner>();
            using (StreamReader sr = new StreamReader("Scheduler.txt"))
            {
                string line = string.Empty;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] lines = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    var planner = new Planner();
                    planner.firstName = lines[0];
                    var dateStr = String.Format("{0} {1}", lines[1], lines[2]);
                    var date = DateTime.ParseExact(dateStr, "dd/MM/yyyy hh:mmtt", System.Globalization.CultureInfo.InvariantCulture); //note the date format comes second. Also, in your examples days are first, months are second!
                    planner.dateTime = date;
                    t.Add(planner);
                }
            }
            t = t.OrderBy(l => l.firstName).ThenBy(l => l.dateTime).ToList();
        }
    }

I don't know why you needed a lastName in your Planner class when it's never given in the examples... O_o

Sign up to request clarification or add additional context in comments.

2 Comments

Sorting date and/or time like a string feels wrong and probably will be wrong. Try sorting new List<string> { "01:03PM", "01:04AM", "01:02AM" }
That's why I said you can parse the data inside the Linq query if needed. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.