jstedfast/MailKit · error · ArgumentException

No sort order provided.

Error message

No sort order provided.

What it means

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.

Solutions

  1. Populate the orderBy list with at least one OrderBy (e.g. new OrderBy (OrderBySortOrder.Ascending, OrderBy.Subject)) before calling Sort.
  2. Guard the call: if (orderBy == null || orderBy.Count == 0) fall back to plain Search or a default sort order.
  3. If no ordering is wanted, use Search instead of Sort.

Example fix

// before
var orderBy = new List<OrderBy>();
var results = folder.Sort(SearchOptions.None, query, orderBy);
// after
var orderBy = new List<OrderBy> { new OrderBy (OrderBySortOrder.Descending, OrderBy.Date) };
var results = folder.Sort(SearchOptions.None, query, orderBy);
Defensive patterns

Strategy: validation

Validate before calling

if (orderBy == null || orderBy.Count == 0)
    orderBy = new List<OrderBy> { new OrderBy (OrderBySortOrder.Descending, OrderBy.Date) };

Try / catch

try {
    results = folder.Sort(options, query, orderBy);
} catch (ArgumentException ex) when (ex.ParamName == "orderBy") {
    results = folder.Search(options, query);
}

Prevention

When it happens

Trigger: Calling Sort(..., new List<OrderBy>()) or passing an empty OrderBy collection (with a non-null query) to any MailFolder.Sort overload.

Common situations: 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.

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


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/07c73d53deec3ded. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/MailFolder.cs:9254

		/// <exception cref="ProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="CommandException">
		/// The command failed.
		/// </exception>
		public virtual SearchResults Sort (SearchOptions options, SearchQuery query, IList<OrderBy> orderBy, PartialRange partialRange, CancellationToken cancellationToken = default)
		{
			if ((options & SearchOptions.All) != 0)
				throw new ArgumentException ("The SearchOptions.All flag cannot be combined with a partial range.", nameof (options));

			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));

			throw new NotSupportedException ("The folder does not support partial sorts.");
		}

		/// <summary>
		/// Asynchronously sort messages matching the specified query, returning only the specified range of results.
		/// </summary>
		/// <remarks>
		/// <para>Asynchronously searches the folder for messages matching the specified query, returning only the
		/// search results within the specified range in the specified sort order.</para>
		/// <para>Positive positions within the <paramref name="partialRange"/> range are relative to the first result
		/// in the sort order while negative positions are relative to the last result. For example, a range of
		/// <c>1:50</c> will return the first 50 results in the specified sort order.</para>
		/// <note type="note">If the range specified by <paramref name="partialRange"/> references results beyond the end
		/// of the complete set of matching messages, then the results will only contain the unique identifiers that
		/// fall within the range (if any).</note>
		/// </remarks>
		/// <returns>The search results.</returns>

View on GitHub (pinned to 9d3859a785)