Exemplo n.º 1
0
        /// <summary>
        /// name:SortByRef
        /// returntype:struct
        /// Descrption:Dates will be sorted by passing value of structure's object
        /// AuthorName:Monal Shah
        /// CreatedDate:2010/09/15
        /// </summary>
        /// <param name="structDates1"></param>
        /// <returns></returns>
        public DateStructFormats1[] SortByValue(DateStructFormats1[] structDates1)
        {
            //  SortedList<DateTime, int> sort2 = new SortedList<DateTime, int>();
            DateTime[] dtDate = new DateTime[structDates1.Length];//dtdate off array is created with structure's object length
            DateTime   dtDate1;

            string[] strS;//strS is object of string

            for (int intJ = 0; intJ < structDates1.Length; intJ++)
            {
                dtDate[intJ] = System.Convert.ToDateTime
                                   (structDates1[intJ].ToString());//here structDates is converted into datetime
                // sort1.Add(date[intJ], intJ);
            }
            for (int intI = 0; intI < structDates1.Length - 1; intI++)
            {
                for (int intJ = intI + 1; intJ < structDates1.Length; intJ++)
                {
                    // if First date is greater than second date than swapping is done and small date is position of one and greater date in second position.
                    if (DateTime.Compare(dtDate[intI], dtDate[intJ]) > 0)
                    {
                        dtDate1      = dtDate[intI];
                        dtDate[intI] = dtDate[intJ];
                        dtDate[intJ] = dtDate1;
                        //sort1.Add(date[intI], intI);
                        //bolSwap = true;
                    }
                }
            }
            Console.WriteLine("Sorted Dates\n");
            for (int intI = 0; intI < structDates1.Length; intI++)
            {
                strS = dtDate[intI].ToString().Split('/');    //Split the datetime's value by "/" and stored in strS
                structDates1[intI] = new DateStructFormats1
                                         (Convert.ToInt32(strS[1]),
                                         Convert.ToInt32(strS[0]),
                                         Convert.ToInt32(strS[2].Substring(0, 4)));//take the value of dates into structDates
                Console.WriteLine("Sorted date:"
                                  + structDates1[intI].ToString());
            }
            return(structDates1);


            //foreach (DateTime date1 in sort2.Keys)
            //{
            //    s = date1.ToString().Split('/');
            //    structDates1[intK] = new DateStructFormats1(Convert.ToInt32(s[0]), Convert.ToInt32(s[1]), Convert.ToInt32(s[2].Substring(0, 4)));
            //    intK++;
            //}



            #endregion
        }
Exemplo n.º 2
0
        /// <summary>
        /// Name:StructDateSort
        /// Author:monal shah
        /// CreatedDate:2010/09/15
        /// </summary>
        public void StructDateSort()
        {
            Console.WriteLine("How Many Dates you want to enter:");
            int       intNumber = System.Convert.ToInt32(Console.ReadLine()); //get the value from the console
            ArrayList arylist   = new ArrayList();                            //create list object of arraylist

            DateStructFormats1[] dtStructFormat;                              //create the object of Structure
            string strTotal1 = "";

            DateStructFormats1[] objDtTotal1;                   //create the object of Structure
            string[]             strSplit;                      //create the array of String
            CommonLogic          objClDate = new CommonLogic(); //CommonLogic Object is created
            int  intday   = 0;
            int  intMonth = 0;
            int  intYear  = 0;
            bool bolFlg;

            dtStructFormat = new DateStructFormats1[intNumber];//create the object of Structure with size of intNumber
            try
            {
                Console.WriteLine("Please Enter First DayValue Second MonthValue and Third YearValue");
                Console.WriteLine("\n");
                for (int intI = 0; intI < intNumber; intI++)
                {
                    Console.WriteLine("Please Enter Day");
                    intday = System.Convert.ToInt32(Console.ReadLine());   //accepting value from the console
                    Console.WriteLine("Please Enter Month");
                    intMonth = System.Convert.ToInt32(Console.ReadLine()); //accepting value from the console
                    Console.WriteLine("Please Enter Year");
                    intYear = System.Convert.ToInt32(Console.ReadLine());  //accepting value from the console
                    dtStructFormat[intI] = new DateStructFormats1
                                               (intday, intMonth, intYear);
                    arylist.Add(dtStructFormat[intI]);             //add value of dtstructFormat into ArrayList's object
                    strTotal1 = dtStructFormat[intI].ToString();   //Convert into tostring format
                    bolFlg    = objClDate.ValidateDate(strTotal1); //ValidateDate method is called by commonlogic object
                    //if bolFlag is true then date will be printed in valid format and add that date into list's object else print message Enter valid Date value
                    if (bolFlg)
                    {
                        Console.WriteLine("\n");
                        Console.WriteLine("Valid date Format:" + strTotal1);
                        lsDate.Add(strTotal1);
                    }
                    else
                    {
                        Console.WriteLine("\n");
                        Console.WriteLine("Enter valid Date value ");
                    }
                }
                if (lsDate.Count > 0)
                {
                    objDtTotal1 = new DateStructFormats1[lsDate.Count];//craete DateStructFormats1's object objDtTotal1 with size of List's object count
                    for (int intI = 0; intI < lsDate.Count; intI++)
                    {
                        strSplit          = lsDate[intI].ToString().Split('/');//Split the list's value by "/" and stored in strSplit
                        objDtTotal1[intI] = new DateStructFormats1
                                                (Convert.ToInt32(strSplit[1]),
                                                Convert.ToInt32(strSplit[0]),
                                                Convert.ToInt32(strSplit[2]));//take the value of dates into objDtTotal1
                    }
                    Console.WriteLine("\n");
                    Console.WriteLine("Original dates in MM/DD/YYYY Format");
                    for (int intJ = 0; intJ < lsDate.Count; intJ++)
                    {
                        Console.WriteLine("Dates:" + objDtTotal1[intJ]);
                    }
                    Console.WriteLine("\n");
                    Console.WriteLine("Traverse Format");
                    Console.WriteLine("\n");
                    Console.WriteLine("Sort by Reference");
                    objClDate.SortByRef(ref objDtTotal1);//here sortByRef is called from commonlogic class with reference of objDtTotal1
                    Console.WriteLine("\n");
                    Console.WriteLine("Traverse Format");
                    Console.WriteLine("\n");
                    Console.WriteLine("Sort by Value");
                    objClDate.SortByValue(objDtTotal1);//here sortByRef is called from commonlogic class with reference of objDtTotal1
                    Console.WriteLine("\n");
                }
            }

            catch (Exception ExException)
            {
                Console.WriteLine("ERROR:" + ExException.Message);//exception
            }
            finally
            {
                lsDate         = null;
                arylist        = null;
                dtStructFormat = null;
                objDtTotal1    = null;
                strSplit       = null;
                objClDate      = null;
            }
        }