The following is a technique I am using in one of my programs that will allow me to sort on any one of multiple columns. This technique requires me to have one or more integer item(s) in a record (a class or structure) that can be accessed directly from the record. This solution will require modification to support data that is not integer type and will require significant improvements to support multiple data types if required.
There are more than a dozen articles in the Listview Control section of the CodeGuru web site that are all relatively advanced, but they might help too.
One important thing is that the item's data must be set to something that the callback can use for the sort. The documentation does not make that clear. Setting the item data to the index values of the items is not likely to work since the index values change during the sorting process. In this example, I set each item's data to a pointer to the record that contains the original data from which the list control item has been loaded, as in the following:
SetItemData(Index, (DWORD)pRecord);
Then when you sort:
SortItems((PFNLVCOMPARE)ComparePositions, (LPARAM)(pField - pRecord));
Which uses the following callback to sort on the field pointed to by pField:
int CALLBACK CListControl::ComparePositions(int pRec1, int pRec2, LPARAM Offset) {
int Pos1, Pos2;
Pos1 = *(int *)(((char *)pRec1)+(int)Offset);
Pos2 = *(int *)(((char *)pRec2)+(int)Offset);
if (Pos1 < Pos2)
return -1;
else
if (Pos1 > Pos2)
return 1;
return 0;
}
See my Visual C++ Programmer Stuff page for more C++ stuff.