February 16, 2019
Hot Topics:

Managed Extensions: Sorting Your Collection Classes

  • August 27, 2004
  • By Tom Archer
  • Send Email »
  • More Articles »

The Client Side

At this point, the client side of things is very simple. In order to enumerate the list, simple follow the instructions explained in the previous article and to sort the collection, you need only call the collection object's Sort method passing it the appropriate sort enum value. For example, this article's demo has an mixed mode application (MFC & .NET) that displays a collection of articles in a listview control. When the user clicks on any of the listview control's header columns, the list is then sorted. Here's that particular function:
void CCollectionSortingDlg::OnHdnItemclickList1(NMHDR *pNMHDR, LRESULT *pResult)
  LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
  switch( phdr->iItem )
    case 0: 
      m_strSortedBy = _T("Title");

    case 1:
      m_strSortedBy = _T("Author");

    case 2:
      m_strSortedBy = _T("Category (DESC)");


  *pResult = 0;

Dynamically Handling Additions to the Collection

The last thing I'll cover is how to dynamically deal with new data being added to the collection. The client code (the user interface) filled the listview control by enumerating the collection of Article objects. However, it would be extremely inefficient to do that each time a new Article object is added to the collection. A much better mechanism is to add a method to the ArticleCollection class that will return the actual (sorted) index of the newly added Article object. As the Article objects are stored in an ArrayList object and the ArticleList class implements a method called IndexOf, our work is mostly done for us.

In the following code snippet, you can see the following additions:

  • ArticleCollection::Add—allows the client to add a new Article object
  • ArticleCollection::IndexOf—Allows the client to determine the index of the newly added Article object. Note that the collection is re-sorted such that the index is based on the current sort order:
__gc class ArticleCollection : public IEnumerable
  int Add(Object* value)
    return articles->Add(value);

  int IndexOf(Article* article)
    return articles->IndexOf(article);
With these two new ArticleCollection methods, the client can use code similar to the following in order to insert the new Article data into the listview control at the proper place:
void CCollectionSortingDlg::OnBnClickedAdd()
  // Add to collection

  // Ask collection where new item was inserted
  int i = m_articles->IndexOf(article);

  // Based on item's insertion point, insert
  // item into list control at same index
  int idx = m_lstArticles.InsertItem(i, m_strTitle);
  m_lstArticles.SetItemText(idx, 1, m_strAuthor);
  m_lstArticles.SetItemText(idx, 2, m_strCategory);

Looking Ahead

At this point, you've seen how to make your classes enumerable and how to sort those enumerable collections. However, one issue that you might have noticed in the last section of this article is that we're assuming that only one client will be using a given ArticleCollection object. In other words, while this code works perfectly in a situation where a single ArticleCollection instance is being used by a single client, we would run into problems if multiple clients were using the same ArticleCollection as each time data was added to the collection (or the collection was sorted), it would muck things up for the other clients. This gets into the issue of versioning—the ability to uniquely identify instances of the collection object such that one client doesn't invalidate the integrity of the collection object for other clients that are concurrently using it. Therefore, the subject of versioning collections will be covered in the next article.

Download the Code

To download the accompanying source code for this article, click here.

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date