{"record":{"id":"07c73d53deec3ded","repo":"jstedfast/MailKit","slug":"no-sort-order-provided","errorCode":null,"errorMessage":"No sort order provided.","messagePattern":"No sort order provided\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/MailFolder.cs","lineNumber":9254,"sourceCode":"\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"CommandException\">\n\t\t/// The command failed.\n\t\t/// </exception>\n\t\tpublic virtual SearchResults Sort (SearchOptions options, SearchQuery query, IList<OrderBy> orderBy, PartialRange partialRange, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif ((options & SearchOptions.All) != 0)\n\t\t\t\tthrow new ArgumentException (\"The SearchOptions.All flag cannot be combined with a partial range.\", nameof (options));\n\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\tthrow new NotSupportedException (\"The folder does not support partial sorts.\");\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously sort messages matching the specified query, returning only the specified range of results.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Asynchronously searches the folder for messages matching the specified query, returning only the\n\t\t/// search results within the specified range in the specified sort order.</para>\n\t\t/// <para>Positive positions within the <paramref name=\"partialRange\"/> range are relative to the first result\n\t\t/// in the sort order while negative positions are relative to the last result. For example, a range of\n\t\t/// <c>1:50</c> will return the first 50 results in the specified sort order.</para>\n\t\t/// <note type=\"note\">If the range specified by <paramref name=\"partialRange\"/> references results beyond the end\n\t\t/// of the complete set of matching messages, then the results will only contain the unique identifiers that\n\t\t/// fall within the range (if any).</note>\n\t\t/// </remarks>\n\t\t/// <returns>The search results.</returns>","sourceCodeStart":9236,"sourceCodeEnd":9272,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailFolder.cs#L9236-L9272","documentation":"Sort requires a non-empty ordering specification; an IList<OrderBy> that is null was already handled, but a list with zero entries gives the sort nothing to order by, so MailFolder throws ArgumentException('No sort order provided.') for the orderBy parameter.","triggerScenarios":"Calling Sort(..., new List<OrderBy>()) or passing an empty OrderBy collection (with a non-null query) to any MailFolder.Sort overload.","commonSituations":"Building the orderBy list dynamically from user-selected sort columns and calling Sort before any column is added; deserializing sort settings that end up empty.","solutions":["Populate the orderBy list with at least one OrderBy (e.g. new OrderBy (OrderBySortOrder.Ascending, OrderBy.Subject)) before calling Sort.","Guard the call: if (orderBy == null || orderBy.Count == 0) fall back to plain Search or a default sort order.","If no ordering is wanted, use Search instead of Sort."],"exampleFix":"// before\nvar orderBy = new List<OrderBy>();\nvar results = folder.Sort(SearchOptions.None, query, orderBy);\n// after\nvar orderBy = new List<OrderBy> { new OrderBy (OrderBySortOrder.Descending, OrderBy.Date) };\nvar results = folder.Sort(SearchOptions.None, query, orderBy);","handlingStrategy":"validation","validationCode":"if (orderBy == null || orderBy.Count == 0)\n    orderBy = new List<OrderBy> { new OrderBy (OrderBySortOrder.Descending, OrderBy.Date) };","typeGuard":null,"tryCatchPattern":"try {\n    results = folder.Sort(options, query, orderBy);\n} catch (ArgumentException ex) when (ex.ParamName == \"orderBy\") {\n    results = folder.Search(options, query);\n}","preventionTips":["Always seed the orderBy list with a default OrderBy before dynamic additions","Validate orderBy.Count > 0 before calling Sort/SortAsync","Use Search instead of Sort when no ordering is required"],"tags":["mailkit","argument-exception","sort","orderby"],"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"}