Sắp xếp list trong C# Bài 1 >> list.sort()
Exam [01] Sort List C# 2016
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Student : IComparable<Student> { public string Name { set; get; } public int Age { set; get; } public Student() { } public Student(string name, int age) { Name = name; Age = age; } public int CompareTo(Student other) { if (this.Name.CompareTo(other.Name) == 0) { if (Age > other.Age) return 1; //Age lớn hơn if(Age < other.Age) return -1; //Age nhỏ hơn if(Age == other.Age) return 0; //Hai đối tượng coi như bằng nhau } return this.Name.CompareTo(other.Name); } } class Program { static void Main(string[] args) { List<Student> list = new List<Student>() { new Student("Nong Long",12), new Student("Nong Long",22), new Student("Nong Long",62), new Student("Nong Long",11) }; Console.WriteLine("Truoc khi sap xep"); foreach (Student std in list) { Console.WriteLine(std.Name+";"+std.Age); } list.Sort(); Console.WriteLine("Sau khi sap xep"); foreach(Student std in list){ Console.WriteLine(std.Name+"; "+std.Age); } int st = list.BinarySearch(new Student("duyet", 30)); Console.WriteLine(); Console.ReadLine(); } } }
Sắp xếp list trong C# Bài 2 >> list.Sort(new CompareStudent());
Exam [02] Sort List C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Student : IComparable<Student>
{
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string clas { get; set; }
public Student(string _firstname, string _lastname, string _email, string _clas)
{
firstname = _firstname;
lastname = _lastname;
email = _email;
clas = _clas;
}
int IComparable<Student>.CompareTo(Student other)
{
throw new NotImplementedException();
}
}
class CompareStudent : IComparer<Student>
{
int IComparer<Student>.Compare(Student x, Student y)
{
if (x == null || x.firstname == null)
{
return -1; //Nếu x null coi như y lớn hơn
}
if (y == null || y.firstname == null)
{
return 1; //Nếu y null thì coi như x lớn hơn
}
if (x.firstname == y.firstname)
{
if (x == null || x.lastname == null)
{
return -1; //Tương tự
}
if (y == null || y.lastname == null)
{
return 1; //Tương tự
}
return x.lastname.CompareTo(y.lastname);
}
return x.firstname.CompareTo(y.firstname);
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>();
list.Add(new Student("Xuan", "Nguyen", "abc@123", "FPT-Aptech"));
list.Add(new Student("Duyen", "Do", "abc@123", "FPT-Aptech"));
list.Add(new Student("Linh", "Pham", "abc@123", "FPT-Aptech"));
list.Add(new Student("Binh", "Bui", "abc@123", "FPT-Aptech"));
list.Add(new Student("Anh", "Nam", "abc@123", "FPT-Aptech"));
list.Sort(new CompareStudent());
foreach (Student stu in list)
{
Console.WriteLine(stu.firstname + " " + stu.lastname + " " + stu.email + " " + stu.clas);
}
Console.ReadLine();
}
}
}
list.Sort();
Exam [03] Sort List C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Employee : IComparable<Employee>
{
public int Salary { get; set; }
public string Name { get; set; }
public int CompareTo(Employee other)
{
// Alphabetic sort if salary is equal. [A to Z]
if (this.Salary == other.Salary)
{
return this.Name.CompareTo(other.Name);
}
// Default to salary sort. [High to low]
return other.Salary.CompareTo(this.Salary);
}
public override string ToString()
{
// String representation.
return this.Salary.ToString() + "," + this.Name;
}
}
class Program
{
static void Main()
{
List<Employee> list = new List<Employee>();
list.Add(new Employee() { Name = "Steve", Salary = 10000 });
list.Add(new Employee() { Name = "Janet", Salary = 10000 });
list.Add(new Employee() { Name = "Andrew", Salary = 10000 });
list.Add(new Employee() { Name = "Bill", Salary = 500000 });
list.Add(new Employee() { Name = "Lucy", Salary = 8000 });
// Uses IComparable.CompareTo()
list.Sort();
// Uses Employee.ToString
foreach (var element in list)
{
Console.WriteLine(element);
}
Console.ReadLine();
}
}
}
0 nhận xét:
Post a Comment