Click here to Skip to main content
14,638,759 members
Articles » Desktop Development » List Controls » ListView controls
Posted 17 Aug 2009

Tagged as


58 bookmarked

ListView Group Sorter

Rate this:
5.00 (9 votes)
Please Sign up or sign in to vote.
5.00 (9 votes)
17 Aug 2009CPOL
An easy to use ListView group sorter (very simple).
Sorted Ascending

Image 1

Sorted Descending



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


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);
        return string.Compare(((ListViewGroup)y).Header, ((ListViewGroup)x).Header);

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)
        List<listviewgroup> lvgs = new List<listviewgroup>();
        foreach (ListViewGroup lvg in _listview.Groups)
        lvgs.Sort(new ListViewGroupHeaderSorter(ascending));

    #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();



  • 17. Aug. 2009 - First version posted.


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


About the Author

Comments and Discussions

GeneralItem sort Pin
Alan Obee21-Jan-14 0:34
MemberAlan Obee21-Jan-14 0:34 
GeneralRe: Item sort Pin
Paw Jershauge22-Jan-14 10:13
MemberPaw Jershauge22-Jan-14 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.