Esempio n. 1
0
        /// <summary>
        /// read file, transfer data format and then save into a List
        /// </summary>
        public static List <NameAndScore> ReadFileAndSaveIntoList(String filePath)
        {
            List <NameAndScore> listOfNameAndScore = new List <NameAndScore>();

            try
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        NameAndScore nameAndScore = new NameAndScore();
                        String[]     items        = line.Split(',');
                        nameAndScore.FirstName = items[0];
                        nameAndScore.LastName  = items[1];
                        nameAndScore.Score     = Convert.ToInt32(items[2]);
                        listOfNameAndScore.Add(nameAndScore);
                    }
                }
                return(listOfNameAndScore);
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Orders the names by their score. If scores are the same, order by their last name followed by
 //// first name
 /// </summary>
 public static Boolean compareScoreAndName(NameAndScore item1, NameAndScore item2)
 {
     return((item1.Score < item2.Score) || (item1.Score == item2.Score && string.Compare(item1.LastName, item2.LastName) < 0) || (item1.Score == item2.Score && string.Compare(item1.LastName, item2.LastName) == 0 && string.Compare(item1.FirstName, item2.FirstName) < 0));
 }