public string getShifts() { Employee empl = new Employee(); //List<string> tempList = new List<string>(); Shift thisweekshift = new Shift(); Shift tempSh = new Shift(); thisweekshift = tempSh.getShiftObjects(); List <Employee> employee = empl.getEmployeeObjects(); List <ShiftsEmployee> tempSE = new List <ShiftsEmployee>(); //Create Sort List to Make the Different Algoritm int plusN = 1; SortedList <double, Employee> sl = new SortedList <double, Employee>(); foreach (Employee emp in employee) { Random rnd = new Random(); double plus = Convert.ToDouble(rnd.Next(1, 50)) * 0.0001; if (emp.First_name != "בר") { double keys = (double)emp.Minimum / (emp.Last_shift.Count()); sl.Add(keys + plus * plusN, emp); plusN++; } else { sl.Add(0, emp); } } //Create new List of employees List <Employee> employeeList = new List <Employee>(); foreach (KeyValuePair <double, Employee> item in sl) { if (item.Value.First_name != "בר") { employeeList.Add(item.Value); } } //Adding the mananger the the end of list foreach (KeyValuePair <double, Employee> item in sl) { if (item.Value.First_name == "בר") { employeeList.Add(item.Value); } } //Revese the list employeeList.Reverse(); //////////// //Algoritm// //////////// foreach (Employee emp in employeeList) //Running on employees list { if (emp.Minimum > emp.Have) //Running just on employees who dont have enough shifts { foreach (ShiftsEmployee shift in emp.Last_shift) //Running in shift list of each employee { for (int i = 0; i < thisweekshift.Possibles.Count; i++) //Running on possible shifts of employees { if (shift.Type == thisweekshift.Shifts[i] && thisweekshift.Possibles[i] > 0) //Match shifts and updating { thisweekshift.Possibles[i] -= 1; emp.Have++; tempSE.Add(shift); } } } } } //Checking that all shifts are done employeeList.Reverse(); for (int i = 0; i < thisweekshift.Possibles.Count; i++) //Running on possible shifts of employees { if (thisweekshift.Possibles[i] > 0) //Match shifts and updating { foreach (Employee emp in employeeList) { foreach (ShiftsEmployee shift in emp.Last_shift) { if (shift.Type == thisweekshift.Shifts[i] && thisweekshift.Possibles[i] > 0) //Match shifts and updating { int temp = 0; foreach (ShiftsEmployee shifts in tempSE) { if (shifts.Title == emp.First_name && shifts.Type == thisweekshift.Shifts[i]) { temp++; } } if (temp == 0) { thisweekshift.Possibles[i] -= 1; emp.Have++; tempSE.Add(shift); } } } } } } ShiftsEmployee SE = new ShiftsEmployee(); string p = SE.writeToJson(tempSE); return(p); }