Ok so I have a number of methods that look like this:- which sorts a list by artist, album, year etc.
public void SortByAlbum(SortOrder sortOrder)
{
if (sortOrder == SortOrder.Ascending)
_list = _list.OrderBy(x => x.Album).ToList();
else if (sortOrder == SortOrder.Descending)
_list = _list.OrderByDescending(x => x.Album).ToList();
}
and this:
public void SortByArtist(SortOrder sortOrder)
{
if (sortOrder == SortOrder.Ascending)
_list = _list.OrderBy(x => x.Artist).ToList();
else if (sortOrder == SortOrder.Descending)
_list = _list.OrderByDescending(x => x.Artist).ToList();
}
Now obviously this isn't good code so it needs refactoring into one Sort() method but I just cant figure out how to do it in the simplest possible way. I don't care if it uses IComparer or LINQ.
I want it to look something like this:
public void Sort(SortOrder sortOrder, SortType sortType)
{
//implementation here
}
public enum SortType
{
Artist,
Album,
Year
}
So whats the cleanest way to do this with no code repetition?
Thanks, Lee