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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:07:27+00:00 2026-05-13T12:07:27+00:00

I have a List<ListViewItem> and I’ve used it in a ListView in VirtualMode There

  • 0

I have a List<ListViewItem> and I’ve used it in a ListView in VirtualMode
There are 5000 items in the list and I’m gonna sort it with LINQ and OrderBy whenever a Column of the ListView is clicked , so I have written below code :


List<ListViewItem> ListOfItems = new List<ListViewItem>();
ListViewColumnSorter lvwColumnSorter;

void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
    try
    {
        // Determine if clicked column is already the column that is being sorted.
        if (e.Column == lvwColumnSorter.SortColumn)
        {
            // Reverse the current sort direction for this column.
            if (lvwColumnSorter.Order == SortOrder.Ascending)
            {
                lvwColumnSorter.Order = SortOrder.Descending;
            }
            else
            {
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
        }
        else
        {
            // Set the column number that is to be sorted; default to ascending.
            lvwColumnSorter.SortColumn = e.Column;
            lvwColumnSorter.Order = SortOrder.Ascending;
        }

        if (lvwColumnSorter.Order == SortOrder.Descending)
            this.ListOfItems.OrderByDescending(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order));
        else
            this.ListOfItems.OrderBy(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order));

        this.listView1.VirtualListSize = ListOfItems.Count;
        this.listView1.Refresh();
    }
    catch { }
}

public class CompareListItemsClass : IComparer<ListViewItem>
{
    private CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
    private SortOrder OrderOfSort;
    private int ColumnToSort;

    public CompareListItemsClass(int columnIndex, SortOrder sortOrder)
    {
        ColumnToSort = columnIndex;
        OrderOfSort = sortOrder;
    }

    public int Compare(ListViewItem listviewX, ListViewItem listviewY)
    {
        // Compare the two items
        try
        {
            compareResult = ObjectCompare.Compare(
                int.Parse(listviewX.SubItems[ColumnToSort].Text),
                int.Parse(listviewY.SubItems[ColumnToSort].Text));
        }
        catch
        {
            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;
        }
    }

    int IComparer<ListViewItem>.Compare(ListViewItem x, ListViewItem y)
    {
        throw new NotImplementedException();
    }
}

but nothing happen ! I expect at least an exception was throw !
Could you please guide me ?
Thanks.

  • 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-13T12:07:27+00:00Added an answer on May 13, 2026 at 12:07 pm

    LINQ methods doesn’t affect the collection, they return a new collection. You can use the ToList method to turn the result into a list and assign it back to the list variable:

    this.ListOfItems = this.ListOfItems.OrderBy(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order)).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have List I want to sort Desc by Priority, which is int and
i have list of images and on mouse over there is option box shows
I have List with n items. I wish transform my list to new list,
I've got a ListView and, for View = List, would like to have the
I'm using a ListView to show Items in a List. The user can select
I have a System.Windows.Forms.ListView control that I was using with View = View.List .
What I have: A custom listview with a custom adapter, with a custom ListViewItem
I bind List of items to WPF ListView and want to set row background/foreground
I have a binding for ListViewItem, which is under gridview/listview/scrollviewer. <Setter Property=IsSelected Value={Binding IsSelected}/>
I have a custom control that is derived from ListView . This list view

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.