首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请问:ArrayList中怎么进行日期排序

2012-01-11 
请教:ArrayList中如何进行日期排序?数据库中有一表结构如下:IDTime12007-08-0822007-08-0932007-08-184200

请教:ArrayList中如何进行日期排序?
数据库中有一表结构如下:
ID       Time
1         2007-08-08
2         2007-08-09
3         2007-08-18
4         2007-08-17
我查询出来的ArrayList日期值分别为:2007-8-8,2007-8-9,2007-8-18,2007-8-17
现拟按日期排序,请教如何实现?
请勿用SQL,因为出来的为字符类型

[解决办法]
使用IComparer,看看MSDN的例子:

using System;
using System.Collections;

public class SamplesArrayList {

public class myReverserClass : IComparer {

// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}

}

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The " );
myAL.Add( "quick " );
myAL.Add( "brown " );
myAL.Add( "fox " );
myAL.Add( "jumps " );
myAL.Add( "over " );
myAL.Add( "the " );
myAL.Add( "lazy " );
myAL.Add( "dog " );

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values: " );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine( "After sorting with the default comparer: " );
PrintIndexAndValues( myAL );

// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( myComparer );
Console.WriteLine( "After sorting with the reverse case-insensitive comparer: " );
PrintIndexAndValues( myAL );

}

public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( "\t[{0}]:\t{1} ", i++, obj );
Console.WriteLine();
}

}


/*
This code produces the following output.
The ArrayList initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog

After sorting with the default comparer:
[0]: brown
[1]: dog
[2]: fox
[3]: jumps
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The

After sorting with the reverse case-insensitive comparer:
[0]: the
[1]: The
[2]: quick
[3]: over
[4]: lazy
[5]: jumps
[6]: fox
[7]: dog
[8]: brown
*/



[解决办法]
ArrayList list = new ArrayList();
list.Add( "2007-8-8 ");
list.Add( "2007-8-9 ");
list.Add( "2007-8-18 ");
list.Add( "2007-8-17 ");
list.Sort(new ObjectCompare());



public class ObjectCompare : IComparer
{
public ObjectCompare()
{
}

/// <summary>
///
/// </summary>
/// <param name= "x "> </param>
/// <param name= "y "> </param>
/// <returns> </returns>
public int Compare(object x, object y)
{
try
{
DateTime a = Convert.ToDateTime(x);
DateTime b = Convert.ToDateTime(y);
return a.CompareTo(b);
}
catch
{
throw;
}
}
}
[解决办法]
用泛型
List <DateTime> list = new List <DateTime> ();

list.Add(DateTime.Parse( "2007-8-8 "));
list.Add(DateTime.Parse( "2007-8-9 "));
list.Add(DateTime.Parse( "2007-8-18 "));
list.Add(DateTime.Parse( "2007-8-17 "));
list.Sort();
这时的list已经是个排序的list

热点排行