4

I have a C# ArrayList with objects that includes following fields. I have around 10 objects in the ArrayList. My problem is I need to sort the ArrayList based on the StartDate available in the system which is a DateTime variable.

public int id { get; set; }
public string title {get; set;}
public bool allDay { get; set; }
public string start { get; set; }
public string end { get; set; }
public string color { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string Location { get; set; }
public string StartDatestr { get; set; }
public string EndDatestr { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public bool Alert { get; set; }
public bool Repeat { get; set; }
public Int32 RepeatDays { get; set; }
public Int32 CalendarID { get; set; }
public int CustomerNo { get; set; }
public string CustomerName { get; set; }
public bool IsProspect { get; set; }
public string Description { get; set; }

Can someone please enlighten me? I tried .Sort() but it just doesn’t do anything (its dump to try sort on ArrayList which has objects I know).

Thanks in advance.

Update:

List<Appointment> appointments = new List<Appointment>();

I created the List as above. Now can I sort it?

4
  • 5
    Do you really need ArrayList(unless say you are on 1.1), can't you use List<yourclass> ? Commented Dec 10, 2012 at 7:25
  • I updated the question can u plz check? Commented Dec 10, 2012 at 8:08
  • 1
    var sortedList = appointments.OrderBy(a=>a.startDate).ToList(); Commented Dec 10, 2012 at 8:13
  • i alredy answer that check my OR option in my answer you can make use of "OrderBy" to do it Commented Dec 10, 2012 at 8:17

2 Answers 2

6

Try by creating comparere like as below

public class ComparerDateTime : IComparer
{
    public int Compare(object x, object y)
    {
        MYCLASS X = x as MYCLASS;
        MYCLASS Y = y as MYCLASS;

        return X.date.CompareTo(Y.date);
    }
}

 MYCLASSList.Sort(new ComparerDateTime ());

OR

Create Generic collection than you can do this

give try to Enumerable.OrderBy operation will do work for you..you need to include linq for this

IEnumerable<Pet> query = arrayList .OrderBy(x=> x.startDate );

if list is created like as below than try linq option

List<MYCLASS> arrayList  = new List<MYCLASS>();
Sign up to request clarification or add additional context in comments.

4 Comments

That's not going to work on just ArrayList, which isn't generic.
@MayuMayooresan - i laredy answer that check my OR option in my answer you can make use of "OrderBy" to do it
When I try OrderBy i'm getting an error Error 4 'System.Collections.Generic.List<Rah.BusinessEntities.Appointment>' does not contain a definition for 'OrderBy' and no extension method 'OrderBy' accepting a first argument of type 'System.Collections.Generic.List<Rah.BusinessEntities.Appointment>' could be found (are you missing a using directive or an assembly reference?)
@MayuMayooresan - you need to add linq name space which is "using System.Linq;" ....
1

You should try making that set of properties into a single class and then create a generic List<NewClass> and you can then use IComparable and IComparer to sort by a particular field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.