{"record":{"id":"0b12e1a52c294c17","repo":"jstedfast/MailKit","slug":"no-sort-order-provided-imapfoldersearch","errorCode":null,"errorMessage":"No sort order provided.","messagePattern":"No sort order provided\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":1544,"sourceCode":"\t\tpublic virtual async Task<SearchResults> SortAsync (string query, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tvar ic = QueueSortCommand (query, cancellationToken);\n\n\t\t\tawait Engine.RunAsync (ic).ConfigureAwait (false);\n\n\t\t\treturn ProcessSortResponse (ic);\n\t\t}\n\n\t\tImapCommand QueueSortCommand (SearchQuery query, IList<OrderBy> orderBy, CancellationToken cancellationToken, out string? charset)\n\t\t{\n\t\t\tif (query == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (query));\n\n\t\t\tif (orderBy == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (orderBy));\n\n\t\t\tif (orderBy.Count == 0)\n\t\t\t\tthrow new ArgumentException (\"No sort order provided.\", nameof (orderBy));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.Sort) == 0)\n\t\t\t\tthrow new NotSupportedException (\"The IMAP server does not support the SORT extension.\");\n\n\t\t\tvar args = new List<object> ();\n\t\t\tvar optimized = query.Optimize (new ImapSearchQueryOptimizer ());\n\t\t\tvar expr = BuildQueryExpression (optimized, args, out charset);\n\t\t\tvar order = BuildSortOrder (orderBy);\n\t\t\tvar command = new StringBuilder (\"UID SORT \");\n\n\t\t\tif ((Engine.Capabilities & ImapCapabilities.ESort) != 0)\n\t\t\t\tcommand.Append (\"RETURN (ALL) \");\n\n\t\t\tcommand.Append (order);\n\t\t\tcommand.Append (' ');\n\t\t\tcommand.Append (charset ?? \"US-ASCII\");","sourceCodeStart":1526,"sourceCodeEnd":1562,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L1526-L1562","documentation":"MailKit throws this ArgumentException from the SearchQuery-based ImapFolder.Sort overload when the orderBy list is null-checked earlier and then found empty. A UID SORT command requires at least one sort criterion; an empty list would yield an invalid command, so the library rejects it before touching the network.","triggerScenarios":"Calling folder.Sort(new List<OrderBy>(), query) or passing an OrderBy collection that was built conditionally and ended up empty; also the async equivalent.","commonSituations":"User-configurable sort preferences where the user deselected all sort fields; deserializing sort settings from config/JSON that produced an empty list.","solutions":["Ensure orderBy contains at least one OrderBy entry (e.g. OrderBy.Date with SortOrder.Descending) before calling Sort.","Default to a sensible ordering (like reverse chronological) when the list is empty.","Validate at the configuration/UI layer that at least one sort key is always selected."],"exampleFix":"// before\nfolder.Sort (orderBy, query); // orderBy may be empty\n\n// after\nif (orderBy.Count == 0)\n    orderBy.Add (new OrderBy (OrderByItem.Date, SortOrder.Descending));\nfolder.Sort (orderBy, query);","handlingStrategy":"validation","validationCode":"if (orderBy == null || orderBy.Count == 0)\n    orderBy = new List<OrderBy> { new OrderBy (OrderByItem.Date, SortOrder.Descending) };\nfolder.Sort (orderBy, query);","typeGuard":null,"tryCatchPattern":"try {\n    ids = folder.Sort (orderBy, query);\n} catch (ArgumentException ex) when (ex.Message.Contains (\"No sort order\")) {\n    ids = folder.Sort (DefaultSortOrder, query);\n}","preventionTips":["Give sort preferences a non-empty default (e.g. reverse-chronological).","Enforce 'at least one sort key' in configuration parsing and UI validation.","Add tests for empty and null orderBy collections."],"tags":["imap","mailkit","argument-error","sort","empty-collection"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}