Update selection after sorting (in LISTVIEW_SortItems) by making sure
selection list matches states of individual items.

diff --git a/dlls/comctl32/listview.c b/dlls/comctl32/listview.c
index 92e5cad..fe5f6fc 100644
--- a/dlls/comctl32/listview.c
+++ b/dlls/comctl32/listview.c
@@ -7382,8 +7382,11 @@
 static LRESULT LISTVIEW_SortItems(HWND hwnd, WPARAM wParam, LPARAM lParam)
 {
     LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongA(hwnd, 0);
-    int nCount;
+    int nCount, i;
     UINT lStyle = GetWindowLongA(hwnd, GWL_STYLE);
+    HDPA *hdpaSubItems=NULL;
+    LISTVIEW_ITEM *pLVItem=NULL;
+
 
     if (lStyle & LVS_OWNERDATA)
       return FALSE;
@@ -7401,6 +7404,20 @@
         DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, hwnd);
     }
 
+    /* Adjust selections so that they are the way they should be after
+       the sort (otherwise, the list items move around, but whatever
+       is at the item's previous original position will be selected instead) */
+    for (i=0; i < nCount; i++)
+    {
+       hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
+       pLVItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0);
+
+       if (pLVItem->state & LVIS_SELECTED)
+          LISTVIEW_AddSelectionRange(hwnd, i, i);
+       else
+          LISTVIEW_RemoveSelectionRange(hwnd, i, i);
+    }
+
     /* align the items */
     LISTVIEW_AlignTop(hwnd);