{"record":{"id":"42c47ea756490536","repo":"jstedfast/MailKit","slug":"no-sort-order-provided-messagesorter","errorCode":null,"errorMessage":"No sort order provided.","messagePattern":"No sort order provided\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/MessageSorter.cs","lineNumber":227,"sourceCode":"\t\t/// <para><paramref name=\"messages\"/> is <see langword=\"null\" />.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"orderBy\"/> is <see langword=\"null\" />.</para>\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <para><paramref name=\"messages\"/> contains one or more items that is missing information needed for sorting.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"orderBy\"/> is an empty list.</para>\n\t\t/// </exception>\n\t\tpublic static IList<T> Sort<T> (this IEnumerable<T> messages, IList<OrderBy> orderBy) where T : IMessageSummary\n\t\t{\n\t\t\tif (messages == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (messages));\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\tvar requiredFields = GetMessageSummaryItems (orderBy);\n\t\t\tvar list = new List<T> ();\n\n\t\t\tforeach (var message in messages) {\n\t\t\t\tif ((message.Fields & requiredFields) != requiredFields)\n\t\t\t\t\tthrow new ArgumentException (\"One or more messages is missing information needed for sorting.\", nameof (messages));\n\n\t\t\t\tlist.Add (message);\n\t\t\t}\n\n\t\t\tif (list.Count < 2)\n\t\t\t\treturn list;\n\n\t\t\tvar comparer = new MessageComparer<T> (orderBy);\n\n\t\t\tlist.Sort (comparer);\n","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MessageSorter.cs#L209-L245","documentation":"MessageSorter.Sort throws ArgumentException when the `orderBy` list is empty (Count == 0). Sorting requires at least one OrderBy criterion; an empty list means no sort order was provided even though a list object was supplied.","triggerScenarios":"Calling `messages.Sort(new List<OrderBy>())`, or passing an orderBy list that ended up empty after filtering/removing criteria, or deserializing an empty sort-order array from configuration.","commonSituations":"Building the sort list from user-selected columns where none were selected; conditions stripping all criteria from the list; a settings file containing an empty sort array.","solutions":["Provide at least one OrderBy criterion: `new List<OrderBy> { OrderBy.Date }`.","Guard before sorting: `if (orderBy == null || orderBy.Count == 0) orderBy = DefaultSortOrder;`.","If the user selected no sort columns, fall back to a sensible default rather than sorting with an empty list."],"exampleFix":"// before\nvar sorted = messages.Sort(orderBy); // orderBy.Count == 0\n// after\nif (orderBy.Count == 0)\n    orderBy.Add(OrderBy.Date);\nvar sorted = messages.Sort(orderBy);","handlingStrategy":"validation","validationCode":"if (orderBy == null || orderBy.Count == 0)\n    throw new InvalidOperationException(\"Provide at least one OrderBy criterion.\");\nvar sorted = messages.Sort(orderBy);","typeGuard":"bool IsValidOrderBy(IList<OrderBy> orderBy) => orderBy != null && orderBy.Count > 0;","tryCatchPattern":"try {\n    return messages.Sort(orderBy);\n} catch (ArgumentException ex) when (ex.ParamName == \"orderBy\") {\n    return messages.Sort(new List<OrderBy> { OrderBy.Date });\n}","preventionTips":["Check orderBy.Count before calling Sort; skip or default when empty.","When criteria come from UI selections, enforce at least one selection server-side.","After filtering criteria, re-validate before sorting."],"tags":["csharp","mailkit","empty-argument","sorting"],"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"}