简体   繁体   English

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

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

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. 这是我尝试过的方法,但是它不断产生错误,指出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>();   

} } }}

I don't like multidimensional arrays - I'll use a List instead. 我不喜欢多维数组-我将使用List 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. 在那里, lines应包含分离和排序的数据。 Note that the sorting is based on alphabetical sorting as everything is still being treated as a string . 请注意,排序基于字母排序,因为所有内容仍被视为string You can parse the data in the Linq query if you need to use specific types. 如果需要使用特定的类型,则可以解析Linq查询中的数据。

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

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 我不知道为什么在示例中从未给出lastName为什么在Planner类中需要lastName ... O_o

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

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