using System;
using System.Collections;
using System.Windows.Forms;
/// <summary>
/// ListView 항목 비교자
/// </summary>
public class ListViewItemComparer : IComparer
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 컬럼 인덱스
/// </summary>
private int columnIndex;
/// <summary>
/// 정렬 순서
/// </summary>
private string sortOrder = "ASC";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 컬럼 인덱스 - ColumnIndex
/// <summary>
/// 컬럼 인덱스
/// </summary>
public int ColumnIndex
{
get
{
return this.columnIndex;
}
set
{
if(value < 0)
{
throw new ArgumentOutOfRangeException("value");
}
this.columnIndex = value;
}
}
#endregion
#region 정렬 순서 - SortOrder
/// <summary>
/// 정렬 순서
/// </summary>
public string SortOrder
{
get
{
return this.sortOrder;
}
set
{
string sortOrder = value.ToUpper();
if(!sortOrder.Equals("ASC") && !sortOrder.Equals("DESC"))
{
throw new ArgumentOutOfRangeException("value");
}
this.sortOrder = sortOrder;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - ListViewItemComparer()
/// <summary>
/// 생성자
/// </summary>
public ListViewItemComparer()
{
ColumnIndex = 0;
}
#endregion
#region 생성자 - ListViewItemComparer(columnIndex, sortOrder)
/// <summary>
/// 생성자
/// </summary>
/// <param name="columnIndex">컬럼 인덱스</param>
/// <param name="sortOrder">정렬 순서</param>
public ListViewItemComparer(int columnIndex, string sortOrder)
{
ColumnIndex = columnIndex;
SortOrder = sortOrder;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 비교하기 - Comparer(object1, object2)
/// <summary>
/// 비교하기
/// </summary>
/// <param name="object1">객체 1</param>
/// <param name="object2">객체 2</param>
/// <returns>처리 결과</returns>
public int Comparer(object object1, object object2)
{
if(this.sortOrder.Equals("ASC"))
{
return string.Compare
(
((ListViewItem)object1).SubItems[this.columnIndex].Text,
((ListViewItem)object2).SubItems[this.columnIndex].Text
);
}
else
{
return string.Compare
(
((ListViewItem)object2).SubItems[this.columnIndex].Text,
((ListViewItem)object1).SubItems[this.columnIndex].Text
);
}
}
#endregion
}
...
/// <summary>
/// 리스트 뷰
/// </summary>
private ListView listView;
...
this.listView.ColumnClick += listView_ColumnClick;
...
#region ListView 컬럼 클릭시 처리하기 - listView_ColumnClick(sender, e)
/// <summary>
/// ListView 컬럼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
for(int i = 0; i < this.listView.Columns.Count; i++)
{
this.listView.Columns[i].Text = this.listView.Columns[i].Text.Replace(" ▽", string.Empty);
this.listView.Columns[i].Text = this.listView.Columns[i].Text.Replace(" △", string.Empty);
}
if(this.listView.Sorting == SortOrder.Ascending || this.listView.Sorting == SortOrder.None)
{
this.listView.ListViewItemSorter = new ListViewItemComparer(e.Column, "DESC");
this.listView.Sorting = SortOrder.Descending;
this.listView.Columns[e.Column].Text = this.listView.Columns[e.Column].Text + " ▽";
}
else
{
this.listView.ListViewItemSorter = new ListViewItemComparer(e.Column, "ASC");
this.listView.Sorting = SortOrder.Ascending;
this.listView.Columns[e.Column].Text = this.listView.Columns[e.Column].Text + " △";
}
this.listView.Sort();
this.listView.ListViewItemSorter = null;
}
#endregion