1

I'm trying to sort dates that are saved in a text document, i have done this using a List. When I try to save the details back into a text document, and inside the document all I see saved is

System.Collections.Generic.List`1[Event_Manager.Form3+MyClass]

This code below is executed with a button, that supposedly sorts the dates, but i can't even see if this code actually sorts the dates, because i keep getting

System.Collections.Generic.List`1[Event_Manager.Form3+MyClass]

saved instead of the original data.

I have tried using .ToString() to prevent this from happening, but it still shows the save output saved into the document.


            // Read the file and display it line by line.
            StreamReader file = new StreamReader("Events.txt");

            List<MyClass> myClassList = new List<MyClass>();

            while ((line = file.ReadLine()) != null)
            {
                string[] split = line.Split(',');

                MyClass myclass = new MyClass();

                myclass.date = DateTime.Parse(split[2]);

                myClassList.Add(myclass);
            }

            file.Close();

            // Sort the list by date
            List<MyClass> myClassListSorted = myClassList.OrderByDescending(x => x.date).ToList();

            using (StreamWriter sr = new StreamWriter(@"Events.txt"))
            {
                foreach (var item in myClassList)
                {
                    sr.WriteLine(myClassListSorted.ToString());

                }
                sr.Close();
            }

I should be able to see the actual data that i originally had saved in the text document, but with sorted dates, rather it just has System.Collections.Generic.List`1[Event_Manager.Form3+MyClass]

Scretqer
  • 25
  • 7
  • As noted in the marked duplicates, when you see the name of a type, you're seeing the default implementation of `ToString()`. When this is a list, nearly always what you _really_ wanted was to somehow represent the list's _contents_ as text, not the list object itself. See marked duplicates for various techniques for achieving that. – Peter Duniho Jul 28 '19 at 04:10
  • Peter - that was only one part of the problem. Read my comment for the second half. – jhilgeman Jul 28 '19 at 04:35

1 Answers1

0

You're looping through the unsorted list and then trying to write the sorted list object (not the items INSIDE the sorted list). Near the end you should be doing this:

foreach (var item in myClassListSorted)
{
     sr.WriteLine(item.date.ToString());
}

Format the date property as needed. Or you can save the original line in each MyClass and then write that line out to the file after sorting.

while ((line = file.ReadLine()) != null)
{
            string[] split = line.Split(',');
            MyClass myclass = new MyClass();
            myclass.date = DateTime.Parse(split[2]);
            MyClass.originalLine = line; // <---------
            myClassList.Add(myclass);
}

And then later in the loop:

     sr.WriteLine(item.originalLine);

That way the data in the text file is the same but just now in sorted order.

jhilgeman
  • 1,543
  • 10
  • 27