Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I sort Integers in a listview

How do I sort columns of integers in a ListView

c#, .net 2.0, Winform

System.Windows.Forms.ListView

like image 609
Brad Avatar asked Jul 31 '09 19:07

Brad


People also ask

How do I sort items in ListView?

Click the various column headers in the ListView control. When you click the header, the contents of the ListView control are sorted in ascending order based on the column that you click. When you click the same column header again, that column is sorted in descending order.

What is a sortable list view?

Lots of objects let you view records in lists, also called “list views”. You can sort the records by one of the field columns. For example, you can sort the All Accounts list view by the Account Name field column, Billing State/Province field column, and others. You can also sort custom list views.

What is a ListView in C#?

C# ListView provides an interface to display a list of items using different kinds of views (e.g., text data, image data, and large images).


2 Answers

This is how I accomplished being able to sort on multiple columns, and being able to sort each column as a number, or as text.

First use this class:

class Sorter : System.Collections.IComparer
{
    public int Column = 0;
    public System.Windows.Forms.SortOrder Order = SortOrder.Ascending;
    public int Compare(object x, object y) // IComparer Member
    {
        if (!(x is ListViewItem))
            return (0);
        if (!(y is ListViewItem))
            return (0);

        ListViewItem l1 = (ListViewItem)x;
        ListViewItem l2 = (ListViewItem)y;

        if (l1.ListView.Columns[Column].Tag == null)
        {
            l1.ListView.Columns[Column].Tag = "Text";
        }

        if (l1.ListView.Columns[Column].Tag.ToString() == "Numeric")
        {
            float fl1 = float.Parse(l1.SubItems[Column].Text);
            float fl2 = float.Parse(l2.SubItems[Column].Text);

            if (Order == SortOrder.Ascending)
            {
                return fl1.CompareTo(fl2);
            }
            else
            {
                return fl2.CompareTo(fl1);
            }
        }
        else
        {
            string str1 = l1.SubItems[Column].Text;
            string str2 = l2.SubItems[Column].Text;

            if (Order == SortOrder.Ascending)
            {
                return str1.CompareTo(str2);
            }
            else
            {
                return str2.CompareTo(str1);
            }
        }
    }
}

In your form's constructor, set the sorter like this:

lvSeries.ListViewItemSorter = new Sorter();

Then handle the ColumnClick even of your listview control like this:

private void lvSeries_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        Sorter s = (Sorter)lvSeries.ListViewItemSorter;
        s.Column = e.Column;

        if (s.Order == System.Windows.Forms.SortOrder.Ascending)
        {
            s.Order = System.Windows.Forms.SortOrder.Descending;
        }
        else
        {
            s.Order = System.Windows.Forms.SortOrder.Ascending;
        }
        lvSeries.Sort();
    }

This is all dependent on the Tag property of each column either being set to "Numeric" or not, so the sorter knows how to sort.

In the above example I cast the values as floats when numeric, you may want to change that to int.

like image 57
Neil N Avatar answered Sep 29 '22 19:09

Neil N


If you are getting started with a ListView, your life will be much much easier if you use an ObjectListView instead. ObjectListView is an open source wrapper around .NET WinForms ListView, and it solves all these annoying little problems that normally make working with a ListView so frustrating. For example, it automatically sorts ints so that '100' comes after '3' (DateTimes, bools, and everything else sorts correctly too).

Seriously, you will never want to go back to a plain ListView after using an ObjectListView.

Yes, I am the author -- but that doesn't mean I'm biased... OK, well maybe it does :) Look here for some other people's opinions.

like image 44
Grammarian Avatar answered Sep 29 '22 19:09

Grammarian