Click here to Skip to main content
12,756,933 members (36,175 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 6 Aug 2002

ListView Sorter

, 22 May 2003
Rate this:
Please Sign up or sign in to vote.
Custom ListViewItemSorter with pluggable cell comparators
<!-- Download Links --> <!-- Add the rest of your HTML here -->


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

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

    //activate sort by that header
    SortBy(_column, this.column ! =_column || 

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
        (this.list.Sorting==SortOrder.Ascending ? 1:-1) *

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


  • 23 May 2003 - updated download


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

Lithuania Lithuania
No Biography provided

You may also be interested in...

Comments and Discussions

NewsBug and fix Pin
dEUs[CPP]25-Apr-06 4:00
memberdEUs[CPP]25-Apr-06 4:00 
GeneralRe: Bug and fix Pin
Mustafa Sait Ozen13-Jul-06 12:27
memberMustafa Sait Ozen13-Jul-06 12:27 
GeneralRe: Bug and fix Pin
Scott S.7-Aug-07 11:36
memberScott S.7-Aug-07 11:36 
GeneralSimilar Approach, But With An Example Pin
FOOS29-Mar-05 13:08
memberFOOS29-Mar-05 13:08 
GeneralProblem with sorting Pin
robdogg13-May-04 9:25
memberrobdogg13-May-04 9:25 
GeneralFrom MSDN Pin
mgvinod13-Jun-03 0:28
membermgvinod13-Jun-03 0:28 
GeneralSorting CheckBoxes Pin
Shardool3-Jun-03 12:14
memberShardool3-Jun-03 12:14 
GeneralListViewSorter Pin
Russell Mangel2-Jan-03 23:35
memberRussell Mangel2-Jan-03 23:35 
GeneralRe: ListViewSorter Pin
demerson14-Mar-05 14:38
memberdemerson14-Mar-05 14:38 
GeneralRe: ListViewSorter Pin
kwirq8-May-06 11:45
memberkwirq8-May-06 11:45 
QuestionListView sorting Mutex? Pin
timothy_M_Miller2-Oct-02 2:43
membertimothy_M_Miller2-Oct-02 2:43 
AnswerRe: ListView sorting Mutex? Pin
Russell Mangel2-Jan-03 23:38
memberRussell Mangel2-Jan-03 23:38 
GeneralRe: ListView sorting Mutex? Pin
timothy_M_Miller7-Jan-03 3:57
membertimothy_M_Miller7-Jan-03 3:57 
GeneralRe: ListView sorting Mutex? Pin
Anonymous11-Jan-03 4:39
sussAnonymous11-Jan-03 4:39 
GeneralRe: ListView sorting Mutex? Pin
timothy_M_Miller22-Jan-03 2:22
membertimothy_M_Miller22-Jan-03 2:22 
GeneralRe: ListView sorting Mutex? Pin
ChristianD20-Jan-03 9:20
memberChristianD20-Jan-03 9:20 
GeneralRe: ListView sorting Mutex? Pin
timothy_M_Miller22-Jan-03 2:35
membertimothy_M_Miller22-Jan-03 2:35 
GeneralRe: ListView sorting Mutex? Pin
ChristianD22-Jan-03 7:15
memberChristianD22-Jan-03 7:15 
GeneralRe: ListView sorting Mutex? Pin
demerson15-Mar-05 9:09
memberdemerson15-Mar-05 9:09 

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.

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