Sorting Multiple Columns

M

MFRASER

Does anyone have a simple example on how to sort using the icomparer class?
I want to be able to sort by two properties of an object.

for example first name lastname.

Here is my code

//Here is my IComparer class that takes the MapItem ojbect the mapItem
object contains first and last name and an ID.



private class MapItemComparer : IComparer

{

private MapItem.MapItemSortColumns m_Column; // Local
MapEntity.VariableSortColumns Enum

private Common.SortOrderTypes m_SortOrder; // Local Sort Order Enum



public MapItemComparer(MapItem.MapItemSortColumns Column,
Common.SortOrderTypes Sort)

{

m_Column = Column;

m_SortOrder = Sort;

}



public int Compare(object x, object y)

{

MapItem MapItemX = (MapItem)x;

MapItem MapItemY = (MapItem)y;

switch (m_Column)

{

case MapItem.MapItemSortColumns.LastName :


switch (m_SortOrder)

{

case Common.SortOrderTypes.Ascending:

return MapItemX.ItemKey.CompareTo(MapItemY.ItemKey);

case Common.SortOrderTypes.Descending:

return MapItemY.ItemKey.CompareTo(MapItemX.ItemKey);

}

break;


case MapItem.MapItemSortColumns.FirstName :


switch (m_SortOrder)

{

case Common.SortOrderTypes.Ascending:

return MapItemX.ItemName.CompareTo(MapItemY.ItemName);

case Common.SortOrderTypes.Descending:

return MapItemY.ItemName.CompareTo(MapItemX.ItemName);

}

break;


}

return MapItemX.ID.CompareTo(MapItemY.ID);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top