c# - Merge sorting days of the week -
i have object array (stockarray) has string property stores days of week. have done merge sort algorithm sort these in order of days of week , not in alphabetical order. have done converting days numbers , storing these in double[] array:
string[] daysarray = new string[5] { "monday", "tuesday", "wednesday", "thursday", "friday" }; (int = 0; < stockarray.length; i++) //stockarray object array { (int j = 0; j < daysarray.length; j++) { if (daysarray[j] == stockarray[i].day) { sortarray[i] = j; break; } } }
i perform merge sort , want output values, have been sorted, days of week format (i.e. "monday", "tuesday" etc), , match correct days of week other properties in object array (stockarray)
string day = ""; int m = 0; while (m < stockarray.length) { foreach (double in sortarray) { if (a == 0) day = "monday"; else if (a == 1) day = "tuesday"; else if (a == 2) day = "wednesday"; else if (a == 3) day = "thursday"; else if (a == 4) day = "friday"; if (day == stockarray[m].day) { console.writeline(stockarray[m].date); console.writeline(stockarray[m].day); console.writeline(stockarray[m].open); console.writeline(stockarray[m].close); console.writeline(stockarray[m].difference); console.writeline(stockarray[m].volume); console.writeline(""); } m++; } } console.readline();
the problem code outputs 31 out of 144 days in stockarray. have checked merge sort algorithm code , seems working error seems in while , foreach loop. doesn't crash - doesn't output elements. if me find error in code appreciated.
try solution:
class stock { public int value; public string day; } dictionary<string, int> dic = new dictionary<string, int>(); dic.add("monday", 0); dic.add("tuesday", 1); dic.add("wednesday", 2); dic.add("thursday", 3); dic.add("friday", 4); var stocks = new stock[] { new stock { day = "tuesday", value= 10 }, new stock { day = "monday", value= 5 }, }; // result here var result = stocks.orderby(f => dic[f.day]).toarray(); foreach (var item in result) { console.writeline(item.day); console.writeline(item.value); }
Comments
Post a Comment