Вот вид предварительной сортировки: предварительно отсортированный список книг
Вот просмотреть сортировку сообщений: отсортированный список книг
Вот XAML для DataTemplate:
Код: Выделить всё
Код: Выделить всё
public async Task FilterAndSortBooks()
{
var result = await Task.Run(() =>
{
IEnumerable filtered = Books;
// Apply filter based on the selected filter option
if (!string.IsNullOrWhiteSpace(SearchText))
{
filtered = SelectedFilter switch
{
"Title" => filtered.Where(book => book.Title.Contains(SearchText, StringComparison.OrdinalIgnoreCase)),
"Author" => filtered.Where(book => book.Author.Contains(SearchText, StringComparison.OrdinalIgnoreCase)),
"Release Year" => filtered.Where(book => book.Year.HasValue && book.Year.Value.ToString("yyyy").Contains(SearchText, StringComparison.OrdinalIgnoreCase)),
_ => filtered
};
}
// Apply sorting based on the selected sort option
filtered = SelectedSortOption switch
{
"Title" => filtered.OrderBy(book => book.Title),
"Author" => filtered.OrderBy(book => book.Author),
"Release Year" => filtered.OrderBy(book => book.Year),
_ => filtered
};
return filtered.ToList();
});
// Update FilteredBooks on the UI thread
Application.Current.Dispatcher.Invoke(() =>
{
FilteredBooks.Clear();
foreach (var book in result)
{
FilteredBooks.Add(book);
}
});
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... after-sort
Мобильная версия