Click here to Skip to main content
Licence 
First Posted 6 Aug 2002
Views 139,447
Downloads 475
Bookmarked 32 times

ListView Sorter

By gz | 22 May 2003
Custom ListViewItemSorter with pluggable cell comparators
6 votes, 37.5%
1
2 votes, 12.5%
2
2 votes, 12.5%
3
3 votes, 18.8%
4
3 votes, 18.8%
5
2.58/5 - 20 votes
μ 3.00, σa 2.84 [?]

Introduction

My solution is an alternative to C# Sorted ListView, posted by Carlos H.Perez. The main difference is that I'm not trying to extend ListView control at all. Instead of that I suggest using a standard ListView with customizable ListViewSorter.

The main idea is to tie ListView column headers with appropriate delegates, which can handle column cells comparison activities. So I declare ListViewSorter.Comparer delegate, and enable to store column-delegate pairs into sorter.

//column cell comparer
public delegate int Comparer(string x, string y);
//comparer for column
public Comparer this[ColumnHeader _column] {...}

The sorter itself is tied to a list control and handles the ColumnClick events for that list. On each column the click sorter activates the clicked column.

//click handler
protected void Column_Click(object sender, ColumnClickEventArgs e)
{
    //get and verify clicked list
    ListView _list = sender as ListView;
    if (_list != this.list) 
       throw new Exception("The event sender does not match the list");

    //sweap columns, which are no longer contained by the list
    CleanUp();

    //get clicked column header
    ColumnHeader _column = this.list.Columns[e.Column];

    //activate sort by that header
    SortBy(_column, this.column ! =_column || 
                    this.list.Sorting!=SortOrder.Ascending);
}

That active column later is used by sorting logic to choose correct comparer and to compare cells.

//activates sort by column
public void SortBy(ColumnHeader _column, bool asc)
{
    //validate column
    if (this.list == null) throw new Exception("The list is null");
    if (_column == null) throw new Exception("The column is null");

    //leav old sort if column is not sortable
    if (this[_column] == null) return;

    //activate column
    this.column = _column;

    //activate sort
    this.list.Sorting = SortOrder.None;
    this.list.Sorting = asc ? SortOrder.Ascending : SortOrder.Descending;
}

Bellow is cell comparison through the Comparer implementation.

//cell comparison
int IComparer.Compare(object x, object y)
{
    //validate list
    if (this.list == null || this.list.Sorting == SortOrder.None 
      || this.column == null) return 0;

    //get cells
    ListViewItem ix = x as ListViewItem;
    ListViewItem iy = y as ListViewItem;
    if (ix.ListView != this.list || iy.ListView != this.list)
        throw new Exception("The event sender does not match the list");

    //compare - use column comparer
    return
        (this.list.Sorting==SortOrder.Ascending ? 1:-1) *
        this[this.column](ix.SubItems[this.column.Index].Text, 
                          iy.SubItems[this.column.Index].Text);
}

ListViewSorter already has standard comparers (as static methods) for strings, numbers, dates.

History

  • 23 May 2003 - updated download

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

gz



Lithuania Lithuania

Member


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
NewsBug and fix PinmemberdEUs[CPP]4:00 25 Apr '06  
GeneralRe: Bug and fix PinmemberMustafa Sait Ozen12:27 13 Jul '06  
GeneralRe: Bug and fix PinmemberScott S.11:36 7 Aug '07  
GeneralSimilar Approach, But With An Example PinmemberFOOS13:08 29 Mar '05  
GeneralProblem with sorting Pinmemberrobdogg19:25 3 May '04  
GeneralFrom MSDN Pinmembermgvinod0:28 13 Jun '03  
GeneralSorting CheckBoxes PinmemberShardool12:14 3 Jun '03  
GeneralListViewSorter PinmemberRussell Mangel23:35 2 Jan '03  
GeneralRe: ListViewSorter Pinmemberdemerson14:38 14 Mar '05  
GeneralRe: ListViewSorter Pinmemberkwirq11:45 8 May '06  
QuestionListView sorting Mutex? Pinmembertimothy_M_Miller2:43 2 Oct '02  
AnswerRe: ListView sorting Mutex? PinmemberRussell Mangel23:38 2 Jan '03  
Hi,
I would like to use this listview sorting code, can you please post a usage example.
I don't understand how to use it.
 
Confused | :confused:
 

 
Russell Mangel
Las Vegas, NV
GeneralRe: ListView sorting Mutex? Pinmembertimothy_M_Miller3:57 7 Jan '03  
GeneralRe: ListView sorting Mutex? PinsussAnonymous4:39 11 Jan '03  
GeneralRe: ListView sorting Mutex? Pinmembertimothy_M_Miller2:22 22 Jan '03  
GeneralRe: ListView sorting Mutex? PinmemberChristianD9:20 20 Jan '03  
GeneralRe: ListView sorting Mutex? Pinmembertimothy_M_Miller2:35 22 Jan '03  
GeneralRe: ListView sorting Mutex? PinmemberChristianD7:15 22 Jan '03  
GeneralRe: ListView sorting Mutex? Pinmemberdemerson9:09 15 Mar '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 23 May 2003
Article Copyright 2002 by gz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid