{"id":112343,"date":"2026-05-20T07:00:00","date_gmt":"2026-05-20T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112343"},"modified":"2026-05-20T21:05:22","modified_gmt":"2026-05-21T04:05:22","slug":"20260520-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260520-00\/?p=112343","title":{"rendered":"The classic TreeView control lets me sort by name or by lParam, but why not both?"},"content":{"rendered":"<p>The Win32 TreeView control in the common controls library provides two ways of sorting elements.<\/p>\n<ul>\n<li><code>TVM_\u00adSORT\u00adCHILDREN<\/code>: Sorts children alphabetically by name.<\/li>\n<li><code>TVM_\u00adSORT\u00adCHILDREN\u00adCB<\/code>: Sorts children via custmm callback.<\/li>\n<\/ul>\n<p>The custom callback is provided the <code>lParam<\/code> of the two tree items being compared. But what if you want to sort by a combination of both the text and the <code>lParam<\/code>? How do you get both?<\/p>\n<p>There are two general designs for using UI controls that represent collections.<\/p>\n<p>One model is for the UI control to be the data repository. Everything you need to know about the item resides in the UI control, somewhere in its name, its check state, its selection state, whatever. If you need to know something about an item, you ask the UI control for the information.<\/p>\n<p>The second model is for the data repository to be some sort of object that itself does not have any UI. (This is known in the biz as a &#8220;data model&#8221;.) You then construct UI elements to be the representation of those objects.<\/p>\n<p>Windows controls generally lean toward the data model approach because there is usually a lot of information about an item that is not present in its UI representation. The data model approach also allows for optimizations in which where very large collections of items create UI elements only for the items that are visible on screen. You can see this in the XAML ListView control as well as in the classic Win32 ListView control when placed into owner-data mode.<\/p>\n<p>For the controls in the common controls library, the general pattern is to provide a place to store a pointer-sized value that is not shown in the UI, typically called &#8220;item data&#8221; or just <code>lParam<\/code>. Here is where you store a pointer to the data model object that the UI object represents.<\/p>\n<p>Okay, so let&#8217;s look at the TreeView sort methods again.<\/p>\n<p>The <code>TVM_\u00adSORT\u00adCHILDREN\u00adCB<\/code> message takes a callback which is passed the <code>lParam<\/code>s of two items to compare. The theory is that these <code>lParam<\/code>s are pointers to larger data structures that describe the item, and you use those larger data structures to decide the ordering of the two items.<\/p>\n<p>The <code>TVM_\u00adSORT\u00adCHILDREN<\/code> message doesn&#8217;t take a callback. It is a convenience method for the case where you are just sorting by name, so it uses the already-available name assigned to the item.<\/p>\n<p>The case where you would need both is the case where the <code>lParam<\/code> is not enough to recover the name, either because it&#8217;s a pointer to a structure that doesn&#8217;t include a name, or because it&#8217;s not a pointer at all.<\/p>\n<p>I can imagine running into this case if the only information you need to track for each TreeView item is its name and a pointer-sized piece of data. You put the name in the TreeView item text and the other data in the <code>lParam<\/code>. This plan works great until you need to sort the items, and your sort comparison function wants access to both pieces of data.<\/p>\n<p>The solution is to switch to a data model pattern. Allocate a structure for each TreeView item and put the string and additional data in that structure. (Alternatively, you could just be sneaky and have the structure be the <code>HTREEITEM<\/code> and the additional data. Then you can recover the string by using the <code>TVM_<wbr \/>GET\u00adITEM<\/code> message.)<\/p>\n<p><b>Bonus chatter<\/b>: In theory, the <code>TVM_\u00adSORT\u00adCHILDREN\u00adCB<\/code> could have passed the <code>HTREEITEM<\/code>s to the callback. The callback could then use the <code>HTREEITEM<\/code> to obtain both the string and the <code>lParam<\/code>. I suspect this didn&#8217;t happen because most callback functions would just ask for the <code>lParam<\/code> from the <code>HTREEITEM<\/code>, <code>TVM_\u00adSORT\u00adCHILDREN\u00adCB<\/code> is doing you a favor and saving you a bunch of work by giving you the thing you probably wanted in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You need to arrange to get one from the other.<\/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-112343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You need to arrange to get one from the other.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112343","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=112343"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112343\/revisions"}],"predecessor-version":[{"id":112344,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112343\/revisions\/112344"}],"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=112343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}