public async Task SortBySortsDestinationsAsExpected(DestinationSorter sorter, string expectedFirstDestinationName) { this.DbContext.Destinations.AddRange(new List <Destination> { new Destination { Id = TestDestinationId, Name = "Aaa", }, new Destination { Id = SecondTestDestinationId, Name = SecondTestDestinationName, }, new Destination { Id = 3, Name = "Some name", }, new Destination { Id = 4, Name = "Another destination", }, }); this.DbContext.Activities.Add(new Activity { DestinationId = 3 }); this.DbContext.Restaurants.Add(new Restaurant { DestinationId = 4 }); await this.DbContext.SaveChangesAsync(); var destinationsToSort = new DestinationViewModel[] { new DestinationViewModel { Id = TestDestinationId, Name = "Aaa", }, new DestinationViewModel { Id = SecondTestDestinationId, Name = SecondTestDestinationName, }, new DestinationViewModel { Id = 3, Name = "Some name", ActivitiesCount = 1 }, new DestinationViewModel { Id = 4, Name = "Another destination", RestaurantsCount = 1 }, }; var actual = this.DestinationsServiceMock.SortBy(destinationsToSort, sorter); Assert.Equal(expectedFirstDestinationName, actual.First().Name); }
public IEnumerable <DestinationViewModel> SortBy(DestinationViewModel[] destinations, DestinationSorter sorter) { switch (sorter) { case DestinationSorter.CountryName: return(destinations.OrderBy(d => d.CountryName).ThenBy(d => d.Name).ToArray()); case DestinationSorter.ActivitiesCount: return(destinations.OrderByDescending(d => d.ActivitiesCount).ToArray()); case DestinationSorter.RestaurantsCount: return(destinations.OrderByDescending(d => d.RestaurantsCount).ToArray()); default: return(destinations.OrderBy(d => d.CountryName).ThenBy(d => d.Name).ToArray()); } }