Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sort Column using CListCtl

Posted on 1998-02-12
3
Medium Priority
?
258 Views
Last Modified: 2008-02-01
I'm using MSVC++4.1.  I have a CListCtl (Report View) class and I can't figure out how to use the SortItems() function with the Callback routine if the user were to click on a column. I was wondering if I could get some coding examples on how to do this.
0
Comment
Question by:kingservant
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1181893
Look at "codeguru" site at http://www.codeguru.com/listview/listview_toc.shtml. There are several article about sorting of list view control
0
 

Author Comment

by:kingservant
ID: 1181894
The examples on that web site didn't quite have the solution I needed.  I guess what I am looking for is examples on using the CListCtrl column click message to invoke my sort using SortItems().
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 240 total points
ID: 1181895
Here is an excerpt from one of my programs:

void CGuidListCtrl::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = reinterpret_cast<NM_LISTVIEW*>(pNMHDR);
      int curSortCol = pNMListView->iSubItem;

      OnColumnClickImp(curSortCol);

      SortFields();
      *pResult = 0;
}

void CGuidListCtrl::SortFields()
{
      CWaitCursor wait;
      SortItems(SortCB, reinterpret_cast<LPARAM>(this));
}

int CALLBACK CGuidListCtrl::SortCB(LPARAM item1, LPARAM item2, LPARAM itemData)
{
      CGuidListCtrl* This = reinterpret_cast<CGuidListCtrl*>(itemData);

      int itemIndex1 = This->GetListIndexOfItemData(item1);
      ASSERT(itemIndex1 != -1);
      CString text1 = This->GetItemText(itemIndex1, This->GetSortColumn());

      int itemIndex2 = This->GetListIndexOfItemData(item2);
      ASSERT(itemIndex2 != -1);
      CString text2 = This->GetItemText(itemIndex2, This->GetSortColumn());

      if (This->IsSortAscending())
            return text1.CompareNoCase(text2);
      else
            return text2.CompareNoCase(text1);

      return 0;
}

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question