Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:04:57+00:00 2026-05-30T16:04:57+00:00

I have a problem. I have an ArrayList of objects. Each object contains a

  • 0

I have a problem.

I have an ArrayList of objects. Each object contains a DateTime data field.

I need to sort this ArrayList by this DateTime data field.

This is my current code and it does not work:

        for (int i = 0;i<EventHolder.Count;i++)
        {
            Event obj = (Event) EventHolder[i];
            try
            {
                obj2=(Event)EventHolder[i+1];
            }
            catch
            {
                break;
            }
            DateTime date1 = DateTime.ParseExact(obj.Date_And_Time,"dd/MM/yyyy HH:mm",region);
            DateTime date2 = DateTime.ParseExact(obj2.Date_And_Time, "dd/MM/yyyy HH:mm", region);
            if (DateTime.Compare(date1,date2)>0)
            {
                Event tempobj=obj2;
                EventHolder[i+1]=obj;
                EventHolder[i]=tempobj;
            }
        }
        foreach (Event i in EventHolder)
        {
            Console.WriteLine(i.Date_And_Time);
        }
        Console.ReadLine();

I use these 3 dates to test if it works:

23/11/2011 12:15

28/06/2010 04:05

02/09/1992 03:54

The output is always:

28/06/2010 04:05

02/09/1992 03:54

23/11/2011 12:15

I tried using just > and < operators for comparing and got same results. I also tried using different commands to convert the strings to DateTime objects, like Convert.ToDateTime, DateTime.Parse. Tried entering without HH:mm (adjusted the code accordingly) and still had the same output. I tried not using IFormatProvider (set it to null) and got nowhere again.

By chance, I found this thread:

How to sort ArrayList of DateTime objects in descending order?

I extracted all the dates from the objects and put them into this DateTime list. Used List.Sort() and it produced correct output. But then I got stuck at how to use this sorted list to sort my EventHolder ArrayList. I’ve been banging on this wall for a while, and produced no result.

I am very new to programming, so I hardly know any good way of doing what I’m doing. And it doesn’t even work.
Can anybody explain to me where I’m going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:05:00+00:00Added an answer on May 30, 2026 at 4:05 pm

    What you’re trying to do there is implement a sorting algorithm but unfortunately yours is not a correct one. It almost looks like Bubble Sort which is a pretty slow algorithm to begin with but it’s not quite correct.

    A much better way to accomplish what you’re trying to do is to use the Sort method or ArrayList or of the generic List<> classes. Those sort methods allow you to provide an IComparer implementation (or a comparison delegate in the case of the generic List) to specify the sort criteria and internally they use a correct and efficient sort algorithm.

    If you use the generic List<> you could just pass in a lambda expression and have it dictate the sorting criteria based on the date field, like so:

    // Assuming EventHolder is a List<Event>
    EventHolder.Sort((d1, d2) => DateTime.ParseExact(d1.Date_And_Time,"dd/MM/yyyy HH:mm",region).CompareTo(DateTime.ParseExact(d2.Date_And_Time,"dd/MM/yyyy HH:mm",region)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList of objects where each object contains a string 'word' and
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a arraylist(eg.CustInfo) which contains a collection of objects(Cust_details,Cust_Auth) for java classes, which
I have problem when I try insert some data to Informix TEXT column via
I have this problem bothering me for days. I'm making a special paint program.
I have different objects which is used to store the data from DB in
I have a problem I created my progress dialog dialog = new ProgressDialog(InstallerActivity.this); I
Im learning Java and having a problem with ArrayList. Firstly I have a class
Using jsf 1.2, hibernate, richfaces 3.3.0GA and facelets. I have this code in my
I am stuck with this following problem. Say, I have a request which has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.