大网数据广告活动
域与目录服务

Sorting the Search Results with IDirectorySearch

· 52CE 我爱测

By default, the results of a search are returned in no guaranteed order. The ADS_SEARCHPREF_SORT_ON preference instructs the server to sort the result set on a specified attribute value before it is returned to the client.

By default, the results of a search are returned in no guaranteed order. The ADS_SEARCHPREF_SORT_ON preference instructs the server to sort the result set on a specified attribute value before it is returned to the client.

It is recommended that indexed attributes be used for sorting. Otherwise, the server must retrieve the complete result set and sort it before sending any results to the client. This also applies to paged searches. Be aware that performance of a sorted search will be increased if the filter includes an indexed attribute and that attribute is specified as the sort key; in this case, Active Directory can satisfy the sort while processing the filter. For example, an efficient sort query for a set of users could have a filter that included (sn>smith) and a sort key of sn.

Server-side sorting with the ADS_SEARCHPREF_SORT_ON search option will reduce the performance of the server. If you will be performing many searches, consider sorting the results manually on the client side to reduce the workload on the server.

By default, result sorting is disabled. To enable result sorting, set an ADS_SEARCHPREF_SORT_ON search option with an ADSTYPE_PROV_SPECIFIC that points to an ADS_SORTKEY structure in the ADS_SEARCHPREF_INFO array passed to the IDirectorySearch::SetSearchPreference method. The ADS_SORTKEY structure is used to specify the attribute to sort on and the order of the sort.

The following code example shows how to enable result sorting.

ADS_SORTKEY SortKey;
SortKey.pszAttrType = L"cn";
SortKey.pszReserved = NULL;
SortKey.fReverseorder = FALSE;

ADS_SEARCHPREF_INFO SearchPref;
SearchPref.dwSearchPref = ADS_SEARCHPREF_SORT_ON;
SearchPref.vValue.dwType = ADSTYPE_PROV_SPECIFIC;
SearchPref.vValue.ProviderSpecific.dwLength = sizeof(SortKey);
SearchPref.vValue.ProviderSpecific.lpValue = (LPBYTE)&SortKey;

Active Directory does not support sorting on constructed attributes, so it is not possible to specify a constructed attribute for sorting. The distinguishedName attribute also cannot be used for sorting. Active Directory also does not allow sorting on more than one attribute, so the ADS_SEARCHPREF_SORT_ON search option can only contain one ADS_SORTKEY structure.

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。