Click here to Skip to main content
12,403,418 members (73,530 online)
Click here to Skip to main content

Stats

50.2K views
976 downloads
52 bookmarked
Posted

An extendable ListViewSorter

, 12 Oct 2006 CPOL
This article describes how to sort a ListView ascending/descending with two lines of code, and how to extend its sorting capabilities.
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using Yaowi.Common.Collections;

namespace Yaowi.Common.Windows.Controls
{
  /// <summary>
  /// Compares items of a the specified column of a ListView.
  /// </summary>
  class ListViewItemComparer : SortComparerBase
  {
    private int col = 0;
    private ISortComparer comparer = null;

    /// <summary>
    /// Constructor.
    /// </summary>
    public ListViewItemComparer()
    {
    }

    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="comparer"></param>
    public ListViewItemComparer(ISortComparer comparer)
    {
      col = 0;
      this.comparer = comparer;
    }

    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="column"></param>
    public ListViewItemComparer(int column)
    {
      col = column;
    }

    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="column"></param>
    /// <param name="comparer"></param>
    public ListViewItemComparer(int column, ISortComparer comparer)
    {
      col = column;
      this.comparer = comparer;
    }

    public ISortComparer Comparer
    {
      get { return comparer; }
      set { comparer = value; }
    }

    /// <summary>
    /// Compare!
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    public override int Compare(object x, object y)
    {
      string x1 = ((ListViewItem)x).SubItems[col].Text;
      string y1 = ((ListViewItem)y).SubItems[col].Text;

      if (comparer == null)
      {
        // Default is the StringComparer
        ISortComparer c = new Yaowi.Common.Collections.StringComparer();
        c.SortOrder = sortorder;
        return c.Compare(x1, y1);
      }
      else
      {
        comparer.SortOrder = sortorder;
        return comparer.Compare(x1, y1);
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Marcus Deecke
Software Developer (Senior)
Germany Germany
I am working for a small software company in Hamburg, Germany, and my day-to-day distress is Java development.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 12 Oct 2006
Article Copyright 2006 by Marcus Deecke
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid