簡體   English   中英

如何在列表項的基礎上對arraylist進行排序

[英]how to sort arraylist on the basis of listitem

我有arraylist,其中listitem包含日期和一些字符串。 我想根據日期對該數組列表進行排序。 我有以下偽代碼用於arraylist項目。

ArrayList _arListDAte = new ArrayList();
//foreach loop for DAtatable rows
ListItem lstitem = new ListItem();
//here is condition for assigning text to lstitem
//if condition match
lstitem.Text = Today.Date() + ' - Allowed'
//else
lstitem.Text = Today.Date() + ' - Not allowed'

listitem.value = datarow[id]+'-M';

所以最后我的Arraylist包含以下數據
文字提示
2013年11月4日-允許120-M
5-01-2013-允許101-M
2013年2月2日-允許121-M
8-8-2012-不允許126-NM

我想按日期升序對這個數組列表進行排序。

定義一個自定義比較器:

public class DateComparer: IComparer  {

int IComparer.Compare( Object x, Object y )  {
    String a = (x as ListItem).Text;
    String b = (y as ListItem).Text;

    DateTime aDate = DateTime.Parse(a.split(null)[0]);
    DateTime bDate = DateTime.Parse(b.split(null)[0]);
    return DateTime.Compare(aDate, bDate);        
  }
}

並在您的代碼后調用它:

IComparer comp = new DateComparer();
arListDAte.Sort(comp);

如果定義一個具有DateTime成員的自定義類而不是使用ListItem類,它將變得更加簡單。 這樣可以避免在處理保存為字符串的DateTime值時出現任何與區域性相關的問題。

static void Main()
{
    ArrayList _arListDAte = new ArrayList();
    CustomListItem listItem = new CustomListItem();
    bool condition = true;
    DateTime date = new DateTime(2013, 04, 15);
    if(condition)
    {
       listItem.Text = date + " - Allowed";
       listItem.Date = date;
    }
    else
    {
       listItem.Text = date + " - Allowed";
       listItem.Date = date;
    }
    _arListDAte.Add(listItem);

    listItem = new CustomListItem();
    date = new DateTime(2013, 04, 13);
    if (condition)
    {
       listItem.Text = date + " - Allowed";
       listItem.Date = date;
    }
    else
    {
       listItem.Text = date + " - Allowed";
       listItem.Date = date;
    }
    _arListDAte.Add(listItem);

    listItem = new CustomListItem();
    date = new DateTime(2013, 04, 18);
    if (condition)
    {
       listItem.Text = date + " - Allowed";
       listItem.Date = date;
    }
    else
    {
       listItem.Text = date + " - Allowed";
       listItem.Date = date;
    }
    _arListDAte.Add(listItem);

    _arListDAte.Sort();

 }

 public class CustomListItem : IComparable
 {
     public string Text { get; set; }
     public string Value { get; set; }
     public DateTime Date { get; set; }

     //Sort Ascending Order
     public int CompareTo(object obj)
     {
         CustomListItem customListItem = obj as CustomListItem;
         if (customListItem != null)
             return this.Date.CompareTo(customListItem.Date);
         return 0;
     }

     // Uncomment for descending sort
     //public int CompareTo(object obj)
     //{
     //    CustomListItem customListItem = obj as CustomListItem;
     //    if (customListItem != null)
     //        return customListItem.Date.CompareTo(this.Date);
     //    return 0;
     //}
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM