public void sortArrayIComparable() //non-static { //http://www.c-sharpcorner.com/uploadfile/yougerthen/using-icomparable-and-icomparer-to-compare-objects/ //Отсортируем по уровню с помощью интерфейса IComparable HistoricalEvent temp = null; int state; bool flag = false; for (int j = 0; j < ob.Length - 1; j++) { for (int i = j; i < ob.Length; i++) { state = ob[j].CompareTo(ob[i]); if (state == 1) { temp = ob[j]; ob[j] = ob[i]; ob[i] = temp; } if (j == ob.Length - 2 && i == ob.Length - 1) { flag = true; Console.WriteLine(ob[j].getYear() + " " + ob[j].getTitle() + " " + ob[j].getCategory()); Console.WriteLine(ob[i].getYear() + " " + ob[i].getTitle() + " " + ob[i].getCategory()); break; } } if (flag != true) Console.WriteLine(ob[j].getYear() + " " + ob[j].getTitle() + " " + ob[j].getCategory()); } }
//Переопределим CompareTo, как сравнение по уровню public int CompareTo(Object obj) { HistoricalEvent temp = (HistoricalEvent)obj; if (String.Compare(this.categori.ToString(), temp.categori.ToString()) == 1) return 1; if (String.Compare(this.categori.ToString(), temp.categori.ToString()) == -1) return -1; else return 0; }
public void addElement(int year, String title, category categori) { int sizeOfArray = ob.Length; ob[sizeOfArray++] = new HistoricalEvent(year, title, categori); }