代码:
while ((linevalue = filereader.ReadLine()) != null)
{
items.Add(linevalue);
}
filereader.Close();
items.Sort();
//To display the content of array (sorted)
IEnumerator myEnumerator = items.GetEnumerator();
while (myEnumerator.MoveNext())
{
Console.WriteLine(myEnumerator.Current);
}
上面的程序显示所有值。如何只提取日期并按升序排序?
我不允许使用linq,使用异常或线程或任何其他东西。我必须坚持使用文件流,尝试从文本文件中获取数据,对其进行排序和存储,以便我可以检索,查看和编辑它,搜索任何特定日期并查看加入记录的日期那个日期。想不通。挣扎
答案 0 :(得分:4)
基本上,不要尝试将文件用作文本行;将它分开,以便你有一段代码将解析该文本转换成类型化的记录,然后在你只需要处理类型化数据时处理它们。
例如(这里我假设文件是制表符分隔的,但您可以将其更改为列索引,而不是很容易);看看我的Main
方法需要做什么一点工作来处理数据:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
static class Program
{
static void Main()
{
foreach (var item in ReadFile("my.txt").OrderBy(x => x.Joined))
{
Console.WriteLine(item.Names);
}
}
static readonly char[] tab = { '\t' };
class Foo
{
public string Names { get; set; }
public int Age { get; set; }
public string Designation { get; set; }
public DateTime Joined { get; set; }
}
static IEnumerable<Foo> ReadFile(string path)
{
using (var reader = File.OpenText(path))
{
// skip the first line (headers), or exit
if (reader.ReadLine() == null) yield break;
// read each line
string line;
var culture = CultureInfo.InvariantCulture;
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split(tab);
yield return new Foo
{
Names = parts[0],
Age = int.Parse(parts[1], culture),
Designation = parts[2],
Joined = DateTime.Parse(parts[3], culture)
};
}
}
}
}
这是一个仅使用ISO-1语言功能的.NET 2.0(以及可能在.NET 1.1上)的版本(不太优雅,但工作正常);我个人认为使用.NET 1.1是愚蠢的,如果你使用的是.NET 2.0,那么List<T>
将比ArrayList
更受欢迎。但这是“最糟糕的情况”:
using System;
using System.Collections;
using System.Globalization;
using System.IO;
class Program
{
static void Main()
{
ArrayList items = ReadFile("my.txt");
items.Sort(FooByDateComparer.Default);
foreach (Foo item in items)
{
Console.WriteLine(item.Names);
}
}
class FooByDateComparer : IComparer
{
public static readonly FooByDateComparer Default
= new FooByDateComparer();
private FooByDateComparer() { }
public int Compare(object x, object y)
{
return ((Foo)x).Joined.CompareTo(((Foo)y).Joined);
}
}
static readonly char[] tab = { '\t' };
class Foo
{
private string names, designation;
private int age;
private DateTime joined;
public string Names { get { return names; } set { names = value; } }
public int Age { get { return age; } set { age = value; } }
public string Designation { get { return designation; } set { designation = value; } }
public DateTime Joined { get { return joined; } set { joined = value; } }
}
static ArrayList ReadFile(string path)
{
ArrayList items = new ArrayList();
using (StreamReader reader = File.OpenText(path))
{
// skip the first line (headers), or exit
if (reader.ReadLine() == null) return items;
// read each line
string line;
CultureInfo culture = CultureInfo.InvariantCulture;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(tab);
Foo foo = new Foo();
foo.Names = parts[0];
foo.Age = int.Parse(parts[1], culture);
foo.Designation = parts[2];
foo.Joined = DateTime.Parse(parts[3], culture);
items.Add(foo);
}
}
return items;
}
}
答案 1 :(得分:0)
我不确定你为什么要只检索日期。您可能最好先将数据读入元组。像
这样的东西List<Tuple<string, int, string, DateTime>> items.
然后你可以按items.Item4对它们进行排序,这将是日期。
答案 2 :(得分:0)
从文件中获取Array中的所需值...
public class DateComparer : IComparer {
public int Compare(DateTime x, DateTime y) {
if(x.Date > y.Date)
return 1;
if(x.Date < y.Date)
return -1;
else
return 0;
}
}
list.Sort(new DateComparer());
答案 3 :(得分:0)
您可以使用LINQ并根据标签拆分行,只检索日期并通过转换到日期进行排序。
while ((linevalue = filereader.ReadLine()) != null)
{
items.Add(linevalue.Split('\t').Last());
}
filereader.Close();
items.OrderBy(i => DateTime.Parse(i));
foreach(var item in items)
{
Console.WriteLine(item);
}