编写了个按文件名大小排序的程序,现在只能对listview中第一列进行排序,我想对第二列进行排序,应该怎么修改,我现在的代码如下:
private void button4_Click(object sender, EventArgs e)
{
string filename;
DirectoryInfo TheFolder = new DirectoryInfo(textBox4.Text);
foreach (FileInfo file in TheFolder.GetFiles())
{
filename = file.Name.Substring(0, file.Name.LastIndexOf(“.”));
ListViewItem listItem = new ListViewItem();
listItem.Checked = true;
listItem.Text = filename;
listItem.SubItems.Add(filename.Substring(filename.Length – 6, 6));
listView1.Items.Add(listItem);
}
}
class ListViewSort : IComparer {
private int col;
private bool descK;
public ListViewSort() {
col = 0;
}
public ListViewSort(int column, object Desc) {
descK = (bool)Desc;
col = column;
}
public int Compare(object x, object y) {
int tempInt = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
if (descK) return -tempInt;
else return tempInt;
}
}
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e){
if (this.listView1.Columns[e.Column].Tag == null)
this.listView1.Columns[e.Column].Tag = true;
bool flag = (bool)this.listView1.Columns[e.Column].Tag;
if (flag)
this.listView1.Columns[e.Column].Tag = false;
else
this.listView1.Columns[e.Column].Tag = true;
this.listView1.ListViewItemSorter = new ListViewSort(e.Column, this.listView1.Columns[e.Column].Tag);
this.listView1.Sort();
private void button4_Click(object sender, EventArgs e)
{
string filename;
DirectoryInfo TheFolder = new DirectoryInfo(textBox4.Text);
foreach (FileInfo file in TheFolder.GetFiles())
{
filename = file.Name.Substring(0, file.Name.LastIndexOf(“.”));
ListViewItem listItem = new ListViewItem();
listItem.Checked = true;
listItem.Text = filename;
listItem.SubItems.Add(filename.Substring(filename.Length – 6, 6));
listView1.Items.Add(listItem);
}
}
class ListViewSort : IComparer {
private int col;
private bool descK;
public ListViewSort() {
col = 0;
}
public ListViewSort(int column, object Desc) {
descK = (bool)Desc;
col = column;
}
public int Compare(object x, object y) {
int tempInt = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
if (descK) return -tempInt;
else return tempInt;
}
}
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e){
if (this.listView1.Columns[e.Column].Tag == null)
this.listView1.Columns[e.Column].Tag = true;
bool flag = (bool)this.listView1.Columns[e.Column].Tag;
if (flag)
this.listView1.Columns[e.Column].Tag = false;
else
this.listView1.Columns[e.Column].Tag = true;
this.listView1.ListViewItemSorter = new ListViewSort(e.Column, this.listView1.Columns[e.Column].Tag);
this.listView1.Sort();
}
我想对第二列进行排序,应该怎么改动上述代码