I don't get how I'm supposed to sort this list by date.. I only know how to sort when its DateTime format or an int. Maybe I'm doing something completly wrong since I'm new.

Do I need to do a second list/array with DateTime? I've tried but I couldn't figure out how to connect it. And when I tried doing a bubble sort it didn't work because of the lists format

List<string[]> msgBoard = new List<string[]>();
string[] info = new string[3];   
Console.WriteLine("\tTitle: ");                        
string title = Console.ReadLine();                       
info[0] = title;                        
Console.WriteLine("\tMessage: ");                        
string msg = Console.ReadLine();                        
info[1] = msg;
string date = DateTime.Now.ToString("yyyy/MM/dd"); 
info[2] = date;                        
msgBoard.Add(info);

All I want to do is to sort the list by using the dates I'll save.

1 Answers

2
Tim On

You really should sort convert that string[] into an object. Consider this code:

private class Entry {
 public string Message { get; set;}
 public string Title { get; set;}
 public DateTime Date { get; set;}
}

...
//your code converted to use this `Entry` object rather than a `string[]`

List<Entry> msgBoard = new List<Entry>();

Entry info = new Entry();    

Console.WriteLine("\tTitle: ");    
string title = Console.ReadLine();    
info.Title = title;    

Console.WriteLine("\tMessage: ");    
string msg = Console.ReadLine();    
info.Message = msg;    

string date = DateTime.Now;    
info.Date = date;    

loggBok.Add(logg);
loggBok.Sort(new Comparison<Entry>((Entry x, Entry y) => x.Date.CompareTo(y.Date)));

BUT if you must sort your list and it has to be an array of strings, you would do it like this (assuming element[2] of each of your strings it the date in yyyy/MM/dd format):

loggBox.Sort(new Comparison<string[]>((string[] x, string[] y)=>
   DateTime.ParseExact(x[2], "yyyy/MM/dd").CompareTo(
   DateTime.ParseExact(y[2], "yyyy/MM/dd"))));