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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:37:46+00:00 2026-05-28T15:37:46+00:00

I am using a generic ListView sorter implementing the IComparer interface. Is this working

  • 0

I am using a generic ListView sorter implementing the IComparer interface.

Is this working on a separate thread from the main thread?

I have some funky results. It Sorts a static ListView just fine, however, once it gets populated with streaming data (it subscribes to some events that constantly add items to it) comparing fails and it becomes funky.

If its on a separate thread – any ideas on how should I modify it so it doesn’t interfere with the populating results (or vice-versa)?

Or if it IS on the same threads, any ideas on why this is happening?

Below is the code for update method that updates the listView (lstTrades)

EDIT: I PASTED THE WRONG CODE ORIGINALLY!!

  private void UpdateList(foo t)
                {
                lstTrades.Items.Add(t.a);
                int i = lstTrades.Items.Count - 1;
                lstTrades.Items[i].SubItems.Add(t.b);
                lstTrades.Items[i].SubItems.Add(t.c.ToString());
                lstTrades.Items[i].SubItems.Add(t.d.ToString());
                lstTrades.Items[i].SubItems.Add(Math.Round(e.pnl, 2).ToString());
                lstTrades.Items[i].SubItems.Add(t.f.ToString());
                lstTrades.Items[i].SubItems.Add(t.g.ToShortTimeString());
                lstTrades.Items[i].SubItems.Add(t.h);
                lstTrades.Items[i].SubItems.Add(t.i.ToString());
                }

The sort code is a gently modified code from http://support.microsoft.com/kb/319401

using System.Collections;
using System.Windows.Forms;
using System;


namespace Aladmin2
{


/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
public class ListViewColumnSorter : IComparer
{
    /// <summary>
    /// Specifies the column to be sorted
    /// </summary>
    private int ColumnToSort;
    /// <summary>
    /// Specifies the order in which to sort (i.e. 'Ascending').
    /// </summary>
    private SortOrder OrderOfSort;
    /// <summary>
    /// Case insensitive comparer object
    /// </summary>
    private CaseInsensitiveComparer ObjectCompare;

    /// <summary>
    /// Class constructor.  Initializes various elements
    /// </summary>
    public ListViewColumnSorter()
    {
        // Initialize the column to '0'
        ColumnToSort = 0;

        // Initialize the sort order to 'none'
        OrderOfSort = SortOrder.None;

        // Initialize the CaseInsensitiveComparer object
        ObjectCompare = new CaseInsensitiveComparer();
    }

    /// <summary>
    /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
    /// </summary>
    /// <param name="x">First object to be compared</param>
    /// <param name="y">Second object to be compared</param>
    /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
    public int Compare(object x, object y)
    {
        int compareResult;
        ListViewItem listviewX, listviewY;

        // Cast the objects to be compared to ListViewItem objects
        listviewX = (ListViewItem)x;
        listviewY = (ListViewItem)y;

        // Compare the two items
        DateTime dateValue;
        if (DateTime.TryParse(listviewX.SubItems[ColumnToSort].Text, out  dateValue))
        {

            compareResult = DateTime.Compare(DateTime.Parse(listviewX.SubItems[ColumnToSort].Text), DateTime.Parse(listviewY.SubItems[ColumnToSort].Text));

        }
        else
        {

            compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
        }

        // Calculate correct return value based on object comparison
        if (OrderOfSort == SortOrder.Ascending)
        {
            // Ascending sort is selected, return normal result of compare operation
            return compareResult;
        }
        else if (OrderOfSort == SortOrder.Descending)
        {
            // Descending sort is selected, return negative result of compare operation
            return (-compareResult);
        }
        else
        {
            // Return '0' to indicate they are equal
            return 0;
        }
    }

    /// <summary>
    /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
    /// </summary>
    public int SortColumn
    {
        set
        {
            ColumnToSort = value;
        }
        get
        {
            return ColumnToSort;
        }
    }

    /// <summary>
    /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
    /// </summary>
    public SortOrder Order
    {
        set
        {
            OrderOfSort = value;
        }
        get
        {
            return OrderOfSort;
        }
    }

}

}

EDIT:

I am only using 1 thread (that I know about/creating) in both updates and sorting

  • 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-28T15:37:47+00:00Added an answer on May 28, 2026 at 3:37 pm

    EDIT: In the article you mentioned, they have the line:

    this.listView1.Sort();

    Try adding this to your ProcessUpdate method, right at the end.

    This sorter doesn’t implement a continuous sort- you need to keep calling that as you add and remove data. Sorry we all jumped on you about threading.

    EDIT #2: Also, try this for your ProcessUpdate method:

    private void UpdateList(foo t)
    {
        // Create the item
        ListViewItem li = new ListViewItem(t.a);
    
        // Add all of the subitems
        li.SubItems.Add(t.b);
        li.SubItems.Add(t.c.ToString());
        li.SubItems.Add(t.d.ToString());
        li.SubItems.Add(Math.Round(e.pnl, 2).ToString());
        li.SubItems.Add(t.f.ToString());
        li.SubItems.Add(t.g.ToShortTimeString());
        li.SubItems.Add(t.h);
        li.SubItems.Add(t.i.ToString());
    
        // Add the item to the list
        lstTrades.Items.Add(li);
    
        // Sort the list
        lstTrades.Sort();
    }
    


    If you are not already, you need to marshal the `ProcessUpdate` method (and anything else that touches a bound list) to the UI thread. You can’t safely update these from a background thread, for the same reasons that you can’t touch UI controls from a background thread.

    But no, the comparer is not running on it’s own thread.

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

Sidebar

Related Questions

I have to work on some code that's using generic lists to store a
I have a generic interface for a mathematics library, something like this: [ContractClass(typeof(MathsDoubleContracts))] public
For example, I have this class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
Hi I have this code using generic and nullable: // The first one is
I have a custom control that is derived from ListView . This list view
While implementing a design using nested generic collections, I stumbled across those limitations apparently
Just got a question about generics, why doesn't this compile when using a generic
I'm new to using generic classes. Here is my question: I have several enumerations
We have a normalized SQL Server 2008 database designed using generic tables. So, instead
I'm using Django's generic year archive view to display event objects by year. 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.