{"id":110316,"date":"2024-09-26T07:00:00","date_gmt":"2024-09-26T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=110316"},"modified":"2024-09-26T11:19:01","modified_gmt":"2024-09-26T18:19:01","slug":"20240926-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240926-00\/?p=110316","title":{"rendered":"If you&#8217;re going to specify the <CODE>LVS_SORTASCENDING<\/CODE> or <CODE>LVS_SORTDESCENDING<\/CODE> style, you had better be telling the truth"},"content":{"rendered":"<p>A customer found that if they created a Win32 ListView control, and then used <code>LVM_<wbr \/>SORT\u00adITEMS<\/code> to sort the items in descending order (presumably in response to a &#8220;reverse sort order&#8221; button somewhere), then <a title=\"How does the classic Win32 ListView handle incremental searching?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240408-00\/?p=109627\"> keyboard searching stopped working<\/a>. Why does sorting a ListView break keyboard searching?<\/p>\n<p>Closer investigation uncovered that they had created the ListView with the <code>LVS_<wbr \/>SORT\u00adASCENDING<\/code> style. When this style is enabled, the ListView control assumes that all the items are sorted ascending in alphabetical order by their label text. When you insert a new item, the ListView uses a binary search to find the insertion point for the new item. And when the user starts typing, it uses a binary search to find the item to select for the incremental search.<\/p>\n<p>If you use the <code>LVM_<wbr \/>SORT\u00adITEMS<\/code> or <code>LVM_<wbr \/>SORT\u00adITEMS\u00adEX<\/code> message to change the order of the items, and the result doesn&#8217;t match the ListView&#8217;s declared sort order, then the ListView&#8217;s binary search algorithm fails (because binary search assumes a sorted list), and incremental searching breaks down.<\/p>\n<p>When the code changes the order of the items, the result needs to match the styles specified on the ListView: If the <code>LVS_<wbr \/>SORT\u00adASCENDING<\/code> style is set, then the items must be sorted ascending. If the <code>LVS_<wbr \/>SORT\u00adDESCENDING<\/code> style is set, then the items must be sorted descending. If neither style is set, then there is no requirement on the order of the items.<\/p>\n<p>In this case, after sorting the items in descending order, the code definitely needs to remove the <code>LVS_<wbr \/>SORT\u00adASCENDING<\/code> style, since the items are no longer sorted ascending. And it may as well add the <code>LVS_<wbr \/>SORT\u00adDESCENDING<\/code> style so that the ListView can take advantage of the new sort order.\u00b9<\/p>\n<p>\u00b9 ListView cannot update the styles automatically because it doesn&#8217;t know what sort criteria your custom sort function uses. I guess it could check whether the result of the sort is ascending or descending and auto-update the styles, but it doesn&#8217;t do that. People who specify <code>LVS_<wbr \/>SORT\u00adASCENDING<\/code> or <code>LVS_<wbr \/>SORT\u00adDESCENDING<\/code> typically just set it and forget it, allowing the ListView to maintain the sorted order when new items are inserted. They don&#8217;t try to override the style by doing a manual sort operation to mess up the ListView&#8217;s carefully groomed item order.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because the listview control uses it to optimize searching.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-110316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because the listview control uses it to optimize searching.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=110316"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110316\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=110316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=110316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=110316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}