Introduction:
To keep this post as concise as possible, let me just say that I need to move all the selected items in the list below a specific (unselected) item.
View documentation listview I came to LVM_SORTITEMSEX message .
Question:
How to use the above message to achieve my goal.
MY EFFORTS TO SOLVE THIS:
So far, using this message, I managed to move all the selected items to the bottom of the list -> listview is sorted so that the non-selected items precede the selected ones.
I just can't figure out how to implement moving selected elements below a specific element.
Below are images of what I get and what I want to achieve:

The left image shows what I get when I use the code below, and the right one shows the result I'm aiming for.
The following are relevant code snippets:
// compare function -> see the documentation int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM hwnd) { LVITEM lvi1 = { 0 }, lvi2 = { 0 }; // get selected state of the first item lvi1.iItem = (int)lParam1; lvi1.iSubItem = 0; lvi1.mask = LVIF_STATE; lvi1.stateMask = LVIS_SELECTED; ListView_GetItem((HWND)hwnd, &lvi1); // get selected state of the second item lvi2.iItem = (int)lParam2; lvi2.iSubItem = 0; lvi2.mask = LVIF_STATE; lvi2.stateMask = LVIS_SELECTED; ListView_GetItem((HWND)hwnd, &lvi2); // if first is selected and second is not selected, swap them if ((lvi1.state & LVIS_SELECTED) && (0 == (lvi2.state & LVIS_SELECTED))) return 1; return 0; } // somewhere in code, on button click for example ListView_SortItemsEx(hwndListView, CompareFunc, hwndListView);
I passed the listview handler as the third parameter to ListView_SortItemsEx , so I can use ListView_GetItem in CompareFunc .
source share