public static object GetDefaultModifier(this PostSortOptions sortOptions, SortOrderOption orderOption) { switch (sortOptions) { case PostSortOptions.Date: return(null); case PostSortOptions.Popularity: if (orderOption == SortOrderOption.Ascending) { return(int.MinValue); } return(int.MaxValue); case PostSortOptions.Price: if (orderOption == SortOrderOption.Ascending) { return(decimal.MinValue); } return(decimal.MaxValue); case PostSortOptions.UserRating: if (orderOption == SortOrderOption.Ascending) { return(0.0m); } return(6.0m); default: throw new ArgumentOutOfRangeException(nameof(sortOptions), sortOptions, null); } }
public async Task <List <PostViewModel> > GetAllPosts(Guid currentUserId, PostSortOptions sortOptions = PostSortOptions.Date, SortOrderOption orderOption = SortOrderOption.Descending, AccomodationOptions?accomodationOptions = null, decimal?minPrice = null, decimal?maxPrice = null, string city = null, string keyword = null) { return((await _context.GetAllPostsAsync(currentUserId, sortOptions, orderOption, accomodationOptions, minPrice, maxPrice, city, keyword)).ToList()); }
/// <summary> /// Sorts twodimensional arrays of indexes by sort criteria. /// Uses results of following methods. /// <see cref="SortClass.GetIndexesMax(int[][])"/> /// <see cref="SortClass.GetIndexesMin(int[][])"/> /// <see cref="SortClass.GetIndexesSum(int[][])"/>. /// </summary> /// <param name="array"> /// Array to sort. /// </param> /// <param name="option"> /// Sort criteria. /// </param> private static void BubbleSort(int[][] array, SortOrderOption option) { for (int i = 0; i < array.Length - 1; i++) { for (int j = i + 1; j < array.Length; j++) { if (array[i][0] > array[j][0] && option == SortOrderOption.Ascend) { int temp_value = array[i][0]; int temp_index = array[i][1]; array[i][0] = array[j][0]; array[i][1] = array[j][1]; array[j][0] = temp_value; array[j][1] = temp_index; } else if (array[i][0] < array[j][0] && option == SortOrderOption.Descend) { int temp_value = array[i][0]; int temp_index = array[i][1]; array[i][0] = array[j][0]; array[i][1] = array[j][1]; array[j][0] = temp_value; array[j][1] = temp_index; } } } }
public async Task <List <PostViewModel> > GetAllPosts(Guid currentUserId, DateTimeOffset date, object pagingModifier, int numberToTake, PostSortOptions sortOptions = PostSortOptions.Date, SortOrderOption orderOption = SortOrderOption.Descending, AccomodationOptions?accomodationOptions = null, decimal?minPrice = null, decimal?maxPrice = null, string city = null, string keyword = null) { return((await _context.GetAllPostsAsync(currentUserId, date, pagingModifier, numberToTake, sortOptions, orderOption, accomodationOptions, minPrice, maxPrice, city, keyword)).ToList()); }
public async Task <IActionResult> GetAllPosts(PostSortOptions sortOptions = PostSortOptions.Date, SortOrderOption orderOption = SortOrderOption.Descending, AccomodationOptions?accomodationOptions = null, decimal?minPrice = null, decimal?maxPrice = null, string city = null, string keyword = null) { var user = await _userManager.GetUserAsync(HttpContext.User); if (user?.ApplicationUserInfo != null && await _userManager.IsInRoleAsync(user, Constants.Authorization.UserRole)) { return(Ok(await _postService.GetAllPosts(user.ApplicationUserInfo.Value, sortOptions, orderOption, accomodationOptions, minPrice, maxPrice, city, keyword))); } return(Ok(await _postService.GetAllPosts(sortOptions, orderOption, accomodationOptions, minPrice, maxPrice, city, keyword))); }
public static IQueryable <Post> SortBy(this IQueryable <Post> source, PostSortOptions sortOptions = PostSortOptions.Date, SortOrderOption orderOption = SortOrderOption.Descending) { switch (sortOptions) { case PostSortOptions.Date: if (orderOption == SortOrderOption.Ascending) { return(source.OrderBy(post => post.PostDate)); } return(source.OrderByDescending(post => post.PostDate)); case PostSortOptions.Popularity: if (orderOption == SortOrderOption.Ascending) { return(source.OrderBy(post => post.Likes.Count)); } return(source.OrderByDescending(post => post.Likes.Count)); case PostSortOptions.Price: if (orderOption == SortOrderOption.Ascending) { return(source.OrderByDescending(post => post.Price.HasValue).ThenBy(post => post.Price)); } return(source.OrderByDescending(post => post.Price.HasValue).ThenByDescending(post => post.Price)); case PostSortOptions.UserRating: if (orderOption == SortOrderOption.Ascending) { return(source.OrderBy(post => post.User.CommentsOnProfile.Average(com => com.Grade))); } return(source.OrderByDescending(post => post.User.CommentsOnProfile.Average(com => com.Grade))); default: throw new ArgumentOutOfRangeException(nameof(sortOptions), sortOptions, null); } }
public async Task <ApiResult <IEnumerable <BasePostViewModel> > > GetAllPosts(DateTimeOffset date, object pagingModifier, int numberToTake, PostSortOptions sortOptions = PostSortOptions.Date, SortOrderOption orderOption = SortOrderOption.Descending, AccomodationOptions?accomodationOptions = null, decimal?minPrice = null, decimal?maxPrice = null, string city = null, string keyword = null) { return(await ApiRequestHelper.HandlApiRequest(async() => { var posts = await _api.GetAllPostsPart(date.ToString("O"), pagingModifier?.ToString(), numberToTake, (int)sortOptions, (int)orderOption, (int?)accomodationOptions, minPrice?.ToString(), maxPrice?.ToString(), city, keyword); return new ApiResult <IEnumerable <BasePostViewModel> >(string.Empty, true, posts.Select(p => new BasePostViewModel(p))); })); }
public SortManager() { SortOptions = PostSortOptions.Date; OrderOption = SortOrderOption.Descending; }
public SortManager(PostSortOptions sortOptions = PostSortOptions.Date, SortOrderOption orderOption = SortOrderOption.Descending) { SortOptions = sortOptions; OrderOption = orderOption; }
/// <summary> /// Sorts jagged array. /// </summary> /// <param name="source_array"> /// Array to sort. /// </param> /// <param name="sortoption"> /// Sort option from enumeration. /// </param> /// <param name="orderoption"> /// Sort order from enumeration. /// </param> /// <returns> /// Sorted array. /// </returns> public static int[][] JuggedArraySort(int[][] source_array, SortOption sortoption, SortOrderOption orderoption) { int[][] indexes_array = null; switch (sortoption) { case SortOption.ByMaxInRow: indexes_array = GetIndexesMax(source_array); break; case SortOption.ByMinInRow: indexes_array = GetIndexesMin(source_array); break; case SortOption.ByRowSum: indexes_array = GetIndexesSum(source_array); break; } BubbleSort(indexes_array, orderoption); int[][] result_array = new int[source_array.Length][]; for (int i = 0; i < source_array.Length; i++) { result_array[i] = source_array[indexes_array[i][1]]; } return(result_array); }