3

I've been trying to figure out how to take next available day based on Present day i.e., if today is Friday, then search in Array for the next nearest day like if Array values are 1[Monday], 2[Tuesday], 4[Thursday], 6[Saturday] then my next day should be Saturday.

Here is what i tried

    //Here i'll get days like 0, 2, 3, 4, 6 pattern, and i'm spliting them based on comma to get single-single day value in array of string 
string[] GetDays = DayInWeek.Split(','); [Here day patterns will change everytime, based on user selection]

//Here i'm looping to take each day and get Enum Text based on Enum Value
foreach (string FirstDay in GetDays)
{ 
    //Here i'm converting the string value into int and passing to DayOfWeek Enum to get respective day
    DayOfWeek DayChoosen = ((DayOfWeek)(Convert.ToInt32(FirstDay)));

    //Here i have my actual day for example Friday
    DayOfWeek StartDay = "Friday";

    //Here i need condition to find next available day in the foreach i.e., after Friday next value should be Saturday, or Sunday, Monday & so on until Friday==Friday
    if (StartDay == DayChoosen)
    {
        //End foreach
    }
}

As i told based on present Day i should find next available day i.e, if Friday i should search for Saturday, if Saturday is not there then Sunday, Monday and so on till Friday=Friday

Amar
  • 407
  • 1
  • 5
  • 24

4 Answers4

3

You don't need all these manipulations with foreach.

You can do the following:

private int nextDay(string dayInWeek, int startDay) 
{
    int[] getDays = dayInWeek.Split(',').Select(int.Parse).ToArray();   

    return getDays.Where(d => d > startDay).Any()
        ? getDays.Where(d => d > startDay).Min()
        : getDays.Min();
}

This algorithm checks if there are any days of a week, which are presented in your array, and come after the startDay. Else, it outputs the first available day in a week.

For example, for a string "0, 2, 3, 4, 6":

  • for startDay 0 - output 2, as it is the minimal integer which is more than 0
  • for startDay 1 - outputs 2
  • for startDay 3 - output 4
  • for startDay 6 it finds no items, which are more than 6, and outputs minimum 0

For string "5" (Friday only):

  • for startDay 5, 6 - finds no items which are more than 5, output minimum (5)
  • for startDay 0-4 - outputs 5, as the minimum number which is greater than 0-4
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Is there any way that i can sort getDays array based on startDay value i.e, suppose getDays = [1,3,4,6] and startDay =3 then getDays should rearrange as [3,4,6,1]... – Amar Dec 18 '15 at 08:33
  • @Rajesh That's another question :) You may sort an array, find the index of your value and shift the array. This article may be helpful: http://stackoverflow.com/questions/1470982/how-to-shift-the-start-of-an-array-in-c – Yeldar Kurmangaliyev Dec 18 '15 at 08:51
  • An equivalent way where the "source" is iterated only once, is `return getDays.OrderByDescending(d => d > startDay).ThenBy(d => d).First();`. One orders the days first by whether they exceed `startDay` or not (`false` is "less than" `true`, so use Descending), and then by the integer itself. Might be harder to read, though, but is cooler. You may want to drop `.ToArray()` in that case. – Jeppe Stig Nielsen Dec 18 '15 at 10:31
0

Try this;

    //Here i'll get days like 0, 2, 3, 4, 6 pattern, and i'm splitting them based on comma to get single-single day value in array of string 
    string DayInWeek = "0, 2, 3, 4, 6";
    string[] GetDays = DayInWeek.Split(','); //[Here day patterns will change everytime, based on user selection]

    DayOfWeek nextAvailableDay;

    //Here i'm looping to take each day and get Enum Text based on Enum Value
    foreach (string FirstDay in GetDays)
    {
        //Here i'm converting the string value into int and passing to DayOfWeek Enum to get respective day
        DayOfWeek DayChoosen = ((DayOfWeek)(Convert.ToInt32(FirstDay)));

        //Here i have my actual day for example Friday
        DayOfWeek StartDay = DayOfWeek.Friday;

        //Here i need condition to find next available day in the foreach i.e., after Friday next value should be Saturday, or Sunday, Monday & so on until Friday==Friday
        if (StartDay.Equals(DayChoosen))
                        break;

        if (StartDay < DayChoosen)
        {
            nextAvailableDay = DayChoosen;
            break;
        }
        continue;
    }
Irshad
  • 3,071
  • 5
  • 30
  • 51
0

You should play on int list. I will provide you the pseudo code.

Algorithm:

  1. sort the available list
  2. get all greater numbers (greater than your current one) from the list
    1. select the minimum now, and break
  3. if you don't have any greater number, select all the minimum numbers (less than your current one)
    1. select maximum from this list now, and break
  4. if you don't have any greater or less numbers, than select the same number and break
  5. now convert the number into week day

It is the possible algorithm I came up to solve this problem. You can convert it into code. May be its not the best one but I am sure it will work fine.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
0

check this:

Console.WriteLine("Kindly provide input");
            string DayInWeek = Console.ReadLine();
            string[] GetDays = DayInWeek.Split(',');
            Array.Sort(GetDays);
            DateTime dt = DateTime.Today;
            int i = (int)dt.DayOfWeek;
            Console.WriteLine("Today is " + (DayOfWeek)i);
            bool flag = false;
            foreach (string FirstDay in GetDays)
            {
                if (Convert.ToInt32(FirstDay) > i)
                {
                    Console.WriteLine((DayOfWeek)Convert.ToInt32(FirstDay) + " is the next day");
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                Console.WriteLine((DayOfWeek)Convert.ToInt32(GetDays[0]) + " is the next day");
            }
            Console.ReadKey();
SKS
  • 220
  • 3
  • 12