list 일반 사용자 정의 정렬 예시

1891 단어
 
  
static void Main(string[] args)
{

    Employee employee = new Employee();
    //
    List employeeList = new List();
    employeeList.Add(new Employee() { EmpId = "001", EmpName = "Tony" });
    employeeList.Add(new Employee() { EmpId = "002", EmpName = "Mack" });
    employeeList.Add(new Employee() { EmpId = "003", EmpName = "Jon" });
    employeeList.Add(new Employee() { EmpId = "004", EmpName = "Dawei" });
    employeeList.Add(new Employee() { EmpId = "005", EmpName = "Jack" });
    employeeList.Add(new Employee() { EmpId = "006", EmpName = "Abby" });
    employeeList.Add(new Employee() { EmpId = "007", EmpName = "Carrie" });
    //
    List toSortList = new List();
    toSortList.Add(new Employee() { EmpId = "003", EmpName = "Jon" });
    toSortList.Add(new Employee() { EmpId = "005", EmpName = "Jack" });
    toSortList.Add(new Employee() { EmpId = "007", EmpName = "Carrie" });
    //
    employeeList.Sort((Employee x, Employee y) => (toSortList.Count(e => e.EmpId == y.EmpId) - toSortList.Count(e => e.EmpId == x.EmpId)));
}

public class Employee
{
    public string EmpId
    {
        get;
        set;
    }

    public string EmpName
    {
        get;
        set;
    }
}

정렬을 거친 후 원본list에서 001002003에 따라 배열된 원소를 003005007001002004...List 맨 앞에 지정된 003005007 정렬

좋은 웹페이지 즐겨찾기