static void Main(string[] args) { Program app = new Program(); Person p1 = new Person("5", 80); Person p2 = new Person("10", 50); Person p3 = new Person("1", 55); Person[] persons = new Person[3]; persons[0] = p1; persons[1] = p2; persons[2] = p3; //ComparerObj cmp = new ComparerObj(Person.ComparePersonWeight); ComparerObj cmp = new ComparerObj(Person.ComparePersonName); app.Sort(persons, cmp); foreach (var item in persons) { Console.WriteLine("Name: " + item.getName()); } Console.ReadKey(); }
void Sort(object[] list, ComparerObj cmp) { for (int i = 0; i < list.Length - 1; i++) { for (int j = i + 1; j < list.Length; j++) { if (cmp(list[i], list[j])) { object temp = list[i]; list[i] = list[j]; list[j] = temp; } } } }
private void btnSort_Click(object sender, EventArgs e) { // Index 0 : Sort by MSSV. // Index 1 : Sort by student name. // Index 2 : Sort by Khoa name. // Index 3 : Sort by Diem TB. if (cbxSort.SelectedIndex == -1) { MessageBox.Show("Please , choose sort method."); return; } SV[] results = svBLL.getAll().ToArray(); switch (cbxSort.SelectedIndex) { case 0: ComparerObj cmp_1 = new ComparerObj(SVBLL.CompareMSSV); this.Sort(results, cmp_1); ShowSortResult(results); break; case 1: ComparerObj cmp_2 = new ComparerObj(SVBLL.CompareStudentName); this.Sort(results, cmp_2); ShowSortResult(results); break; case 2: ComparerObj cmp_3 = new ComparerObj(SVBLL.CompareKhoaName); this.Sort(results, cmp_3); ShowSortResult(results); break; case 3: ComparerObj cmp_4 = new ComparerObj(SVBLL.CompareDiemTB); this.Sort(results, cmp_4); ShowSortResult(results); break; } }