struggling
struggling

Reputation: 535

How to sort the time

I am trying to write a program, which sort the time in very optimized manner.Time is in the format HH:MM:SS I read data from file.txt and the data in file is as below :

01:22:15 07:35:35 11:03:35 08:45:37 16:20:23 14:10:48 11:41:44 23:20:14 07:18:21 15:48:01 19:32:44 05:27:52 00:08:02 08:44:07 19:06:02 23:59:34 17:27:26
12:38:23 22:39:18 07:25:32 08:43:52

I have to read it line by line and print the sorted output line by line. I initially thought of using Dictionary and assigning HH (Hours) to the key and order it by key but the **problem is, What if two Hours are same (i mean 12:56:59 already exist by key 12 and again i try to add 12:23:44 by key 12 ** Definitely exception).

My try to do this is :

class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader reader = File.OpenText("C:\\Users\\ACER\\Desktop\\KT-iNDIA\\zExtra\\Bookprprn\\testCodeEval\\testCodeEval\\file.txt"))

                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (null != line)
                    {
                        Dictionary<string, string> dictHH = new Dictionary<string, string>();

                        string[] digits = line.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string digit in digits)
                        {
                            string[] eachdigit = digit.Split(new char[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            dictHH.Add(eachdigit[0], eachdigit[1] + " " + eachdigit[2]);
                        }

                        foreach (KeyValuePair<string, string> ch in dictHH.OrderBy(k => k.Key).Reverse())
                        {
                            Console.Write("{0} {1}    ", ch.Key, ch.Value);
                        }
                    }
                    Console.WriteLine("");
                }
            Console.ReadKey();
        }
    }

How to solve the issue ?

Upvotes: 0

Views: 78

Answers (1)

StepUp
StepUp

Reputation: 38124

Just use List<T> instead of Dictionary<TKey, TValue> cause you do not have a unique key and you do not use performance advantage of reading data by key in Dictionary<TKey, TValue> .

while (!reader.EndOfStream)
{
    string line = reader.ReadLine();;
    if (null != line)
    {
       List<string> listTime = new List<string>();
       string[] digits = line.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
       foreach (string digit in digits)
       {
          string[] eachdigit = digit.Split(new char[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries);
          //dictHH.Add(eachdigit[0], eachdigit[1] + " " + eachdigit[2]);
          list.Add(eachdigit[0]+ eachdigit[1] + " " + eachdigit[2]);
       }         
       foreach (var c in list.OrderBy(x => x))
          Console.WriteLine(c);
      }          
}

Upvotes: 1

Related Questions