jstedfast/MailKit · error · ArgumentException
No sort order provided.
Error message
No sort order provided.
What it means
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.
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.
Example fix
// before
folder.Sort (orderBy, query); // orderBy may be empty
// after
if (orderBy.Count == 0)
orderBy.Add (new OrderBy (OrderByItem.Date, SortOrder.Descending));
folder.Sort (orderBy, query); Defensive patterns
Strategy: validation
Validate before calling
if (orderBy == null || orderBy.Count == 0)
orderBy = new List<OrderBy> { new OrderBy (OrderByItem.Date, SortOrder.Descending) };
folder.Sort (orderBy, query); Try / catch
try {
ids = folder.Sort (orderBy, query);
} catch (ArgumentException ex) when (ex.Message.Contains ("No sort order")) {
ids = folder.Sort (DefaultSortOrder, query);
} Prevention
- 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.
When it happens
Trigger: Calling folder.Sort(new List<OrderBy>(), query) or passing an OrderBy collection that was built conditionally and ended up empty; also the async equivalent.
Common situations: User-configurable sort preferences where the user deselected all sort fields; deserializing sort settings from config/JSON that produced an empty list.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Cannot sort using an empty query.
- The SearchOptions.All flag cannot be combined with a…
- The IMAP server does not support the SORT extension.
- The IMAP server does not support the ESORT extension.
- No indexes were specified.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/0b12e1a52c294c17.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Imap/ImapFolderSearch.cs:1544
public virtual async Task<SearchResults> SortAsync (string query, CancellationToken cancellationToken = default)
{
var ic = QueueSortCommand (query, cancellationToken);
await Engine.RunAsync (ic).ConfigureAwait (false);
return ProcessSortResponse (ic);
}
ImapCommand QueueSortCommand (SearchQuery query, IList<OrderBy> orderBy, CancellationToken cancellationToken, out string? charset)
{
if (query == null)
throw new ArgumentNullException (nameof (query));
if (orderBy == null)
throw new ArgumentNullException (nameof (orderBy));
if (orderBy.Count == 0)
throw new ArgumentException ("No sort order provided.", nameof (orderBy));
CheckState (true, false);
if ((Engine.Capabilities & ImapCapabilities.Sort) == 0)
throw new NotSupportedException ("The IMAP server does not support the SORT extension.");
var args = new List<object> ();
var optimized = query.Optimize (new ImapSearchQueryOptimizer ());
var expr = BuildQueryExpression (optimized, args, out charset);
var order = BuildSortOrder (orderBy);
var command = new StringBuilder ("UID SORT ");
if ((Engine.Capabilities & ImapCapabilities.ESort) != 0)
command.Append ("RETURN (ALL) ");
command.Append (order);
command.Append (' ');
command.Append (charset ?? "US-ASCII");View on GitHub (pinned to 9d3859a785)