Irshad
Irshad

Reputation: 303

how to sort arraylist on the basis of listitem

I have arraylist which has listitem containing date and some string. I want to sort this Arraylist on the basis of Date. I have following pseudo code for arraylist items.

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';

so finally my Arraylist contains below data
Text VAlue
11-04-2013 - Allowed 120-M
5-01-2013 - Allowed 101-M
2-02-2013 - Allowed 121-M
8-8-2012 - Not Allowed 126-NM

I want to sort this arraylist by date in ascending order.

Upvotes: 2

Views: 694

Answers (2)

Patrick D'Souza
Patrick D'Souza

Reputation: 3573

It becomes simpler, if you define a custom class that has a DateTime member instead of using the ListItem class. This can avoid any culture related issues in processing of DateTime values saved as strings.

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;
     //}
 }

Upvotes: 0

nmat
nmat

Reputation: 7591

Define a custom comparator:

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

And call it after your code:

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

Upvotes: 1

Related Questions