private void btnSort_Click(object sender, EventArgs e) { Dictionary <string, FeedbackSortType> sortTypes = new Dictionary <string, FeedbackSortType>() { { "First Name", FeedbackSortType.FIRST_NAME }, { "Last Name", FeedbackSortType.LAST_NAME }, { "DOB", FeedbackSortType.DOB } }; string selectedSortType = sortTypesList[cboSort.SelectedIndex]; FeedbackSortType sortType = sortTypes[selectedSortType]; if (radBtnAscending.Checked) { feedbackManager.Sort(sortType, ListSortDirection.Ascending); } else { feedbackManager.Sort(sortType, ListSortDirection.Descending); } ShowFirstFeedback(); }
/// <summary> /// Sorts the list by a specific field specify by the user this can /// either be First Name, Last Name, or the DOB of a caller. A long /// with the field to sort the caller must specify the direction of the /// sort either ascending or descending. /// </summary> /// <param name="feedbackSortType">The field which the list is to be sorted (ordered) by</param> /// <param name="sortDirection">The direction of the sort (ascending or descending)</param> public void Sort(FeedbackSortType feedbackSortType, ListSortDirection sortDirection) { if (feedbackSortType == FeedbackSortType.FIRST_NAME) { if (sortDirection == ListSortDirection.Ascending) { feedbacks = feedbacks.OrderBy(o => o.firstName).ToList(); } else { feedbacks = feedbacks.OrderByDescending(o => o.firstName).ToList(); } } if (feedbackSortType == FeedbackSortType.LAST_NAME) { if (sortDirection == ListSortDirection.Ascending) { feedbacks = feedbacks.OrderBy(o => o.lastName).ToList(); } else { feedbacks = feedbacks.OrderByDescending(o => o.lastName).ToList(); } } if (feedbackSortType == FeedbackSortType.DOB) { if (sortDirection == ListSortDirection.Ascending) { feedbacks = feedbacks.OrderBy(o => o.dateOfBirth).ToList(); } else { feedbacks = feedbacks.OrderByDescending(o => o.dateOfBirth).ToList(); } } }