Share via


ModuleListPage.GetItemComparer(ColumnHeader, SortOrder) Method

Definition

Gets an object that compares two columns.

protected:
 virtual System::Collections::IComparer ^ GetItemComparer(System::Windows::Forms::ColumnHeader ^ sortColumn, System::Windows::Forms::SortOrder sortOrder);
protected virtual System.Collections.IComparer GetItemComparer (System.Windows.Forms.ColumnHeader sortColumn, System.Windows.Forms.SortOrder sortOrder);
abstract member GetItemComparer : System.Windows.Forms.ColumnHeader * System.Windows.Forms.SortOrder -> System.Collections.IComparer
override this.GetItemComparer : System.Windows.Forms.ColumnHeader * System.Windows.Forms.SortOrder -> System.Collections.IComparer

Parameters

sortColumn
ColumnHeader

The column that is used to sort rows.

sortOrder
SortOrder

The sort order.

Returns

An object that is derived from the IComparer interface.

Examples

The following example implements the GetItemComparer method.

protected override IComparer GetItemComparer(
    ColumnHeader sortColumn, SortOrder sortOrder) {

    if (sortColumn == _keyHeader) {
        return new
            MyCntEntryItemComparer(sortOrder);
    }

    return base.GetItemComparer(sortColumn, sortOrder);
} 
internal class MyCntEntryItemComparer : IComparer {
    private SortOrder order;

    public MyCntEntryItemComparer() {
        order = SortOrder.Ascending;
    }

    public MyCntEntryItemComparer(SortOrder sortOrder) {
        order = sortOrder;
    }

    public int Compare(Object x, Object y) {
        try {
            MyEntryItem entryX = (MyEntryItem)x;
            MyEntryItem entryY = (MyEntryItem)y;

            if (order == SortOrder.Ascending) {
                return (entryX.Entry.UserCount >
                    entryY.Entry.UserCount ? 1 : 0);
            } else {
                return (entryX.Entry.UserCount <
                    entryY.Entry.UserCount ? 1 : 0);
            }
        } catch (System.Exception e) {
            Trace.WriteLine(e.Message);
            Trace.WriteLine(e.StackTrace);
            return 0;
        }
    }
} 
internal sealed class MyEntryInfo {

    private PropertyBag _bag;
    enum ext { eName, eUserCount };

    public MyEntryInfo(PropertyBag bag) {
        _bag = bag;
    }

    public string Name {
        get {
            return (string)_bag[(int)ext.eName];
        }
        set {
            _bag[(int)ext.eName] = value;
        }
    }

    public int UserCount {
        get {
            object o = _bag[(int)ext.eUserCount];
            if (o == null) {
                return 0;
            }
            return (int)o;
        }
        set {
            _bag[(int)ext.eUserCount] = value;
        }
    }
}

internal class MyEntryItem : ListViewItem {

    private MyEntryInfo _entry;

    public MyEntryItem(MyEntryInfo entry)
        : base(entry.Name, 0) {
        _entry = entry;

        this.SubItems.Add(new ListViewSubItem(
            this, "Local Count"));
    }

    public MyEntryInfo Entry {
        get {
            return _entry;
        }
    }
}

Remarks

The returned object compares two objects from the sort column for equivalence.

Applies to