对ListViewItems列表进行排序

时间:2018-02-05 21:19:41

标签: c# .net list sorting listview

我需要编写一个方法来对listview项列表进行排序 - listOfLVI。 给定 - 列表中的元素数(或maxLines)可能是数千。这就是考虑虚拟列表视图的原因。

public partial class Form1 : Form
{
    private static int maxLines = 5;

    System.Collections.Generic.List<ListViewItem> listOfLVI = new List<ListViewItem>();
    ListViewItem NewListViewItem = null;

    // Populate Items
    private void Form1_Load(object sender, EventArgs e)
    {
        listView.VirtualMode = true;
        listView.VirtualListSize = maxLines;
        toolStripStatusLabel2.Text = maxLines.ToString();

        for (int l = 0; l < maxLines; l++)
        {
            NewListViewItem = new ListViewItem("TestName " + l + "NG");
            listOfLVI.Add(NewListViewItem);
        }
    }

    private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        ListViewItem lvi = new ListViewItem();
        lvi = listOfLVI[e.ItemIndex];

        ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
        lvsi.Text = lvi.Text;
        lvi.SubItems.Add(lvsi);

        e.Item = lvi;
    }

    // Add Item
    private void AddItems_Click(object sender, EventArgs e)
    {
        Console.WriteLine("maxLines: " + maxLines);
        int count = maxLines;
        for (int l = count; l < (count + 5); l++)
        {
            NewListViewItem = new ListViewItem("TestName " + l + "FZZ");
            listOfLVI.Add(NewListViewItem);
            maxLines = maxLines + 1;
        }

        foreach (ListViewItem each in listOfLVI)
        {
            String eachString = each.Text;
        }

        // Need to sort the list now based on TestName.
        listOfLVI.Sort(); 

        listView.VirtualListSize = maxLines;
    }

    // Sort List
}

任何人都可以帮我编写代码来对给定的listview项目列表进行排序吗? QuickSort算法会在这里成为一个不错的选择吗?如果是,有关我们如何实施的建议?

0 个答案:

没有答案