jstedfast/MailKit · error · ArgumentException

No sort order provided.

Error message

No sort order provided.

What it means

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.

Solutions

  1. Provide at least one OrderBy criterion: `new List<OrderBy> { OrderBy.Date }`.
  2. Guard before sorting: `if (orderBy == null || orderBy.Count == 0) orderBy = DefaultSortOrder;`.
  3. If the user selected no sort columns, fall back to a sensible default rather than sorting with an empty list.

Example fix

// before
var sorted = messages.Sort(orderBy); // orderBy.Count == 0
// after
if (orderBy.Count == 0)
    orderBy.Add(OrderBy.Date);
var sorted = messages.Sort(orderBy);
Defensive patterns

Strategy: validation

Validate before calling

if (orderBy == null || orderBy.Count == 0)
    throw new InvalidOperationException("Provide at least one OrderBy criterion.");
var sorted = messages.Sort(orderBy);

Type guard

bool IsValidOrderBy(IList<OrderBy> orderBy) => orderBy != null && orderBy.Count > 0;

Try / catch

try {
    return messages.Sort(orderBy);
} catch (ArgumentException ex) when (ex.ParamName == "orderBy") {
    return messages.Sort(new List<OrderBy> { OrderBy.Date });
}

Prevention

When it happens

Trigger: 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.

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

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/42c47ea756490536. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/MessageSorter.cs:227

		/// <para><paramref name="messages"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="orderBy"/> is <see langword="null" />.</para>
		/// </exception>
		/// <exception cref="System.ArgumentException">
		/// <para><paramref name="messages"/> contains one or more items that is missing information needed for sorting.</para>
		/// <para>-or-</para>
		/// <para><paramref name="orderBy"/> is an empty list.</para>
		/// </exception>
		public static IList<T> Sort<T> (this IEnumerable<T> messages, IList<OrderBy> orderBy) where T : IMessageSummary
		{
			if (messages == null)
				throw new ArgumentNullException (nameof (messages));

			if (orderBy == null)
				throw new ArgumentNullException (nameof (orderBy));

			if (orderBy.Count == 0)
				throw new ArgumentException ("No sort order provided.", nameof (orderBy));

			var requiredFields = GetMessageSummaryItems (orderBy);
			var list = new List<T> ();

			foreach (var message in messages) {
				if ((message.Fields & requiredFields) != requiredFields)
					throw new ArgumentException ("One or more messages is missing information needed for sorting.", nameof (messages));

				list.Add (message);
			}

			if (list.Count < 2)
				return list;

			var comparer = new MessageComparer<T> (orderBy);

			list.Sort (comparer);

View on GitHub (pinned to 9d3859a785)