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 7550009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:06:16+00:00 2026-05-30T10:06:16+00:00

I am trying to sort a ListView which also has a DateTime column. This

  • 0

I am trying to sort a ListView which also has a DateTime column. This is the code I use:

public bool isDate(Object obj)
{
    string strDate = obj.ToString();
    try
    {
        DateTime dt = DateTime.Parse(strDate);
        if (dt != DateTime.MinValue && dt != DateTime.MaxValue)
            return true;
        return false;
    }
    catch
    {
        return false;
    }
}

public int Compare(object o1, object o2)
{
    if (!(o1 is ListViewItem))
        return (0);
    if (!(o2 is ListViewItem))
        return (0);

    ListViewItem lvi1 = (ListViewItem)o2;
    string str1 = lvi1.SubItems[ByColumn].Text;
    ListViewItem lvi2 = (ListViewItem)o1;
    string str2 = lvi2.SubItems[ByColumn].Text;

    int result;
    if (lvi1.ListView.Sorting == SortOrder.Ascending)
    {
        if (isDate(str1) && isDate(str2))
            result = DateTime.Compare(DateTime.Parse(str1), DateTime.Parse(str2));
        else
            result = String.Compare(str1, str2);
    }
    else
        result = String.Compare(str2, str1);

    LastSort = ByColumn;
    return result;
}

The ListView holds about 2000 entries and the problem is that it’s very slow. What am I doing wrong? Any ideas?

Thanks in advance!

Edit: Thank you very much. I am new to this and here is my code now. It is much faster and I fixed my logic.

public int Compare(object o1, object o2)
{
    var lvi1 = o2 as ListViewItem;
    var lvi2 = o1 as ListViewItem;
    if (lvi1 == null || lvi2 == null)
        return 0;

    string str1 = lvi1.SubItems[ByColumn].Text;
    string str2 = lvi2.SubItems[ByColumn].Text;

    int result;
    DateTime dateValue1 = new DateTime();
    DateTime dateValue2 = new DateTime();

    if (lvi1.ListView.Sorting == SortOrder.Ascending)
    {  
        if (DateTime.TryParse(str1, out dateValue1) && DateTime.TryParse(str2, out dateValue2)) 
            result = DateTime.Compare(dateValue1, dateValue2);
        else
            result = String.Compare(str1, str2);
    }
    else
    {
        if (DateTime.TryParse(str1, out dateValue1) && DateTime.TryParse(str2, out dateValue2))
            result = DateTime.Compare(dateValue2, dateValue1);
        else
            result = String.Compare(str2, str1);
    }

    LastSort = ByColumn;
    return result;  
}
  • 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-30T10:06:17+00:00Added an answer on May 30, 2026 at 10:06 am

    Some optimization comes in my mind:

    1. Instead of isDate use:DateTime.TryParse

    2. Instead of doing two time casting:

      if (!(o1 is ListViewItem)) return (0);

      ListViewItem lvi1 = (ListViewItem)o2;

    use one time casting:

    var lvi1 = o2 as ListViewItem;
    if (lvi1 == null)
       return 0;
    

    Also I suggest check your code logic (as I mentioned in comment).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a custom sort with a ListView, as described in this
I'm trying to filter my ListView which is populated with this ArrayAdapter: package me.alxandr.android.mymir.adapters;
I'm trying to sort a listview when the user clicks on the column header.
I'm trying to sort the list items, I'm using custom listview. In this i
I'm trying to sort a collection derived from CollectionViewSource, which simply has SortDescriptions for
I am trying to sort a singly linked list which has been created and
I'm trying to sort this object by the score value so I can populate
Trying to sort this array of objects according to (1) depth and (2) weight,
Been trying to sort this for over a day now, and I am sure
I'm trying to sort an array of people objects. The class looks like this.

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.