Click here to Skip to main content
Licence CPOL
First Posted 17 Aug 2009
Views 22,139
Downloads 756
Bookmarked 44 times

ListView Group Sorter

By Paw Jershauge | 17 Aug 2009
An easy to use ListView group sorter (very simple).

1

2

3

4
6 votes, 100.0%
5
5.00/5 - 6 votes
μ 5.00, σa 1.10 [?]
Sorted Ascending

Sorted Descending

Desc.jpg

Introduction

The code descried here enables you to sort ListView Groups, ascending or descending.

Background

I needed a "quick and dirty" solution to sort my Groups in a ListView. So I came up with this very very simple solution.

Using the code

Just cast your ListView into my listview wrapper and call SortGroup(bool).

Like this where listView1 is your own ListView to be sorted:

((ListViewGroupSorter)listView1).SortGroups(true);  //Ascending...
((ListViewGroupSorter)listView1).SortGroups(false); //Descending...

The ListViewGroupHeaderSorter Class

public class ListViewGroupHeaderSorter : IComparer<ListViewGroup>
{
    private bool _ascending = true;
    public ListViewGroupHeaderSorter(bool ascending)
    {
        _ascending = ascending;
    }

#region IComparer<ListViewGroup> Members

    public int Compare(ListViewGroup x, ListViewGroup y)
    {
    if (_ascending)
        return string.Compare(((ListViewGroup)x).Header, ((ListViewGroup)y).Header);
    else
        return string.Compare(((ListViewGroup)y).Header, ((ListViewGroup)x).Header);
    }
#endregion
}

The ListViewGroupHeaderSorter Class

public class ListViewGroupSorter
{
    internal ListView _listview;

    public static bool operator ==(ListView listview, ListViewGroupSorter sorter)
    {
        return listview == sorter._listview;
    }
    public static bool operator !=(ListView listview, ListViewGroupSorter sorter)
    {
        return listview != sorter._listview;
    }

    public static implicit operator ListView(ListViewGroupSorter sorter)
    {
        return sorter._listview;
    }
    public static implicit operator ListViewGroupSorter(ListView listview)
    {
        return new ListViewGroupSorter(listview);
    }

    internal ListViewGroupSorter(ListView listview)
    {
        _listview = listview;
    }

    public void SortGroups(bool ascending)
    {
        _listview.BeginUpdate();
        List<listviewgroup> lvgs = new List<listviewgroup>();
        foreach (ListViewGroup lvg in _listview.Groups)
            lvgs.Add(lvg);
        _listview.Groups.Clear();
        lvgs.Sort(new ListViewGroupHeaderSorter(ascending));
        _listview.Groups.AddRange(lvgs.ToArray());
        _listview.EndUpdate();
    }

    #region overridden methods

    public override bool Equals(object obj)
    {
        return _listview.Equals(obj);
    }

    public override int GetHashCode()
    {
        return _listview.GetHashCode();
    }

    public override string ToString()
    {
        return _listview.ToString();
    }

    #endregion
}

History

  • 17. Aug. 2009 - First version posted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Paw Jershauge

Software Developer

Denmark Denmark

Member
ListView Group Sorter
An Code Generator, for making SQL table into .Net Classses (2nd place in the Code Generation 2008 Competition)
A Class for getting the Rss feed list of a website
Seagate Date Code Calculator
DNSBL lookup Class
Base N converter (N = 10-62)
Lightweight Directory Access Protocol Uniform resource identifier (LDAPUri)
LoginHours from DirectoryEntry into boolean array

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 17 Aug 2009
Article Copyright 2009 by Paw Jershauge
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid