본문 바로가기

C# Winform

C# Winform | List Sort | ObservableCollection Sort

by C기억저장소 2020. 7. 3.

C# Winform | List Sort | ObservableCollection Sort

 

  List Sort

 

Code - 1

List<ClassObject> orgList = new List<ClassObject>();
List<ClassObject> tempList = orgList.OrderBy(x => x.FiledName).ToList();
cs

 

Code - 2

List<classObject> orgList = new List<classObject>();
orgList.Sort(delegate (classObjectx, classObjecty)
{
    if (x.FiledName == null && y.FiledName == nullreturn 0;
    else if (x.FiledName == nullreturn -1;
    else if (y.FiledName == nullreturn 1;
    else return x.FiledName.CompareTo(y.FiledName);
});
cs

 

  ObservableCollection Sort

 

ObservableCollection<classObject> orgObservableCollection = new ObservableCollection<classObject>();
var tempObservableCollection = orgObservableCollection.OrderByDescending(o => o.FiledName).ToList();
foreach (var temp in tempObservableCollection)
{
    int oldIndex = orgObservableCollection.IndexOf(temp);
    int newIndex = tempObservableCollection.IndexOf(temp);
    orgObservableCollection.Move(oldIndex, newIndex);
}
cs

 

2번째 라인의 정렬방식

var tempObservableCollection = orgObservableCollection.OrderByDescending(o => o.FiledName).ToList();

 

- OrderBy : 오름차순

- OrderByDescending : 내림차순