public static IOrderedEnumerable <MessageRedirect> Sort(this MessageRedirectsCollection source, HttpRequestMessage request, string defaultSortDirection = "desc") { var query = request.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value); if (!query.TryGetValue("direction", out var direction)) { direction = defaultSortDirection; } if (direction != "asc" && direction != "desc") { direction = defaultSortDirection; } if (!query.TryGetValue("sort", out var sort)) { sort = "from_physical_address"; } if (!SortOptions.Contains(sort)) { sort = "from_physical_address"; } if (sort == "to_physical_address") { return(direction == "asc" ? source.Redirects.OrderBy(r => r.ToPhysicalAddress) : source.Redirects.OrderByDescending(r => r.ToPhysicalAddress)); } return(direction == "asc" ? source.Redirects.OrderBy(r => r.FromPhysicalAddress) : source.Redirects.OrderByDescending(r => r.FromPhysicalAddress)); }