I have class named : Book and in this class I have 2 properties : BookName and BookDate like this :
public class Book
{
public string BookName
{
get;
set;
}
public DateTime BookDate
{
get;
set;
}
}
and in the Main I created an instance of it and in the foreach loop I can see the items . like this :
List<Book> books = new List<Book>();
DateTime firstBookTime = new(2011, 02, 02, 2, 2, 2);
DateTime secondBookTime = new(2012, 02, 02, 2, 2, 2);
Book firstBook = new()
{
BookName = "firstBook",
BookDate = firstBookTime
};
Book secondBook = new()
{
BookName = "secondBook",
BookDate = secondBookTime
};
books.Add(firstBook);
books.Add(secondBook);
foreach (Book bookList in books)
{
Console.WriteLine
(
$"name is : {bookList.BookName}" +
$" and date : is {bookList.BookDate} "
);
}
everything is fine I can see he items with console . but I cant sort them , I tried these but no good resault :
books.Sort((x, y) => string.Compare(x.BookName, x.BookName));
books = books.OrderBy(item => item.BookDate).ToList();
books.Sort();
How can I sort the BookName alphabetically and BookDate base on newer date ?
thanks