繁体   English   中英

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

[英]Sorting and Storing in a Multidimensional Array List based on the data retrieved from a text file

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

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

这是通过拆分单词来存储文本文件的全部内容。

我如何做同样的事情,并使以下数据存储在多维数组列表中。 通过排序。

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,因此必须具有主体。

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>();   

}}

我不喜欢多维数组-我将使用List 无论如何,它下面都有一个数组,因此您可以随心所欲地进行操作。 ;)

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

在那里, lines应包含分离和排序的数据。 请注意,排序基于字母排序,因为所有内容仍被视为string 如果需要使用特定的类型,则可以解析Linq查询中的数据。

当然,您可以在此处通过适当的类或您需要的其他任何东西解析数据(到实际的stringDateTime和可能的TimeSpan实例中)。

这是您发布的代码,已更正以与您提供的示例数据一起使用:

    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();
        }
    }

我不知道为什么在示例中从未给出lastName为什么在Planner类中需要lastName ... O_o

暂无
暂无

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

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