C# 基于从文本文件检索的数据在多维数组列表中排序和存储

C# 基于从文本文件检索的数据在多维数组列表中排序和存储,c#,multidimensional-array,C#,Multidimensional Array,我已将以下信息存储在文本文件中。我正在使用文件流和流读取器对象来读取它。我得到了一个数组方法: string[] MyArray = allText.Split(new string[]{" ",Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); 这是通过按单词拆分来存储文本文件的全部内容 我如何做同样的事情,并使以下数据存储在多维arraylist中。通过分类 Princeton

我已将以下信息存储在文本文件中。我正在使用文件流和流读取器对象来读取它。我得到了一个数组方法:

string[] MyArray = allText.Split(new string[]{" ",Environment.NewLine},  
    StringSplitOptions.RemoveEmptyEntries);
这是通过按单词拆分来存储文本文件的全部内容

我如何做同样的事情,并使以下数据存储在多维arraylist中。通过分类

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
把它想象成桌子。所有这些数据都存储在多维数组列表中。 姓名必须被排序和存储,日期必须被排序和存储,时间必须被排序和存储。仅接受每个给定情况下的特定格式


这是我尝试过的,但它不断生成一个错误,指出get和set方法没有被标记为extern或abstract,因此必须有一个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>();   
使用系统;
使用系统集合;
使用System.IO;
使用系统文本;
班级计划员
{public string firstName{get;set;}
公共字符串lastName{get;set;}
公共日期时间日期时间{get;set;}
}
类exe
{    
公共静态void Main(字符串[]args)
{
列表t=新列表();
StreamReader sr=新的StreamReader(“@cScheduler.txt”))
{
string line=string.Empty;
而((line=sr.ReadLine())!=null)
{
string[]line=line.Split(“”).ToArray();
//添加到您的列表中
t、 添加(newtest(){firstName=line[0],lastName=line[1],dateTime=dateTime.ParseExact(“MM/dd/yyyy hh:MM:ss tt”,
第[2]行+第[3]行,
CultureInfo.InvariantCulture)});
} 
}//您的已排序列表现在为t=t.OrderBy(x=>x.dateTime).ToList();
}
}

我不喜欢多维数组-我将使用
列表。它的下面有一个数组,所以你可以随心所欲地使用它。;)


我不知道为什么在
Planner
类中需要
lastName
,而示例中从未给出过它。。。你能举一个多维格式表格的例子吗?我现在无法想象。如果您将
类LineObject{string Name{get;set;}DateTime DateValue{get;set;}}
,将所有行读入该结构中,并使用
列表来保存它们,您的任务可能会更容易一些。然后,您可以
OrderBy(x=>x.Name)
x=>x.DateValue.Date
OrderBy(x=>x.DateValue.Hour)。然后通过(x=>x.DateValue.Minute)
。最后发布的示例代码没有引用上面发布的文本示例。名字?姓?看起来你只有一个名字,然后是一个日期,可能还有一个时间。像字符串一样排序日期和/或时间感觉是错误的,而且可能会是错误的。尝试排序
新列表{“01:03PM”、“01:04AM”、“01:02AM”}
这就是为什么我说,如果需要,可以解析Linq查询中的数据
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
    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();
        }
    }