How to sort a listview column that contains file size data? C#

后端 未结 3 882
时光取名叫无心
时光取名叫无心 2021-01-14 20:59

I want to sort the items inside column of ListView, i already made it, but... i can\'t made it with the type of data in column (see picture), someone knows the way for do it

3条回答
  •  死守一世寂寞
    2021-01-14 21:30

    Create listViewSizeSorter class extending Icomparer interface as below.

    using System.Collections;
    using System.Windows.Forms;
    
    namespace MyNameSpace
    {
        /// 
        /// This class is an implementation of the 'IComparer' interface.
        /// 
        public class ListViewSizeSorter : IComparer
        {
            /// 
            /// Specifies the column to be sorted
            /// 
            private int _columnToSort;
            /// 
            /// Specifies the order in which to sort (i.e. 'Ascending').
            /// 
            private SortOrder _orderOfSort;
    
            /// 
            /// Class constructor.  Initializes various elements
            /// 
            public ListViewSizeSorter()
            {
                // Initialize the column to '0'
                SortColumn = 0;
    
                // Initialize the sort order to 'none'
                Order = SortOrder.None;
            }
    
            /// 
            /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
            /// 
            /// First object to be compared
            /// Second object to be compared
            /// The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'
            public int Compare(object x, object y)
            {
                var listviewX = (ListViewItem)x;
                var listviewY = (ListViewItem)y;
                string strX = listviewX.SubItems[_columnToSort].Text;
                string strY = listviewY.SubItems[_columnToSort].Text;
    
                // Nulls first (null means less, since it's blank)
                if (strX == null)
                {
                    if (strY == null)
                        return 0;
                    return -1;
                }
                if (strY == null)
                    return 1;
    
                // Convert the non-KB part to a number
                double numX = 0;
                double numY = 0;
                if (strX.EndsWith("KB") || strX.EndsWith("GB") || strX.EndsWith("MB"))
                    double.TryParse(strX.Substring(0, strX.Length - 3), out numX);
                if (strX.EndsWith("Bytes"))
                    double.TryParse(strX.Substring(0, strX.Length - 6), out numX);
                if (strY.EndsWith("KB") || strY.EndsWith("GB") || strY.EndsWith("MB"))
                    double.TryParse(strY.Substring(0, strY.Length - 3), out numY);
                if (strY.EndsWith("Bytes"))
                    double.TryParse(strX.Substring(0, strY.Length - 6), out numY);
                long bytesX;
                long bytesY;
                if (strX.EndsWith("KB"))
                    bytesX = (long)numX * 1024;
                else if (strX.EndsWith("MB"))
                    bytesX = (long)numX * 1048576;
                else if (strX.EndsWith("GB"))
                    bytesX = (long)numX * 1073741824;
                else
                    bytesX = (long) numX;
    
                if (strY.EndsWith("KB"))
                    bytesY = (long)numY * 1024;
                else if (strY.EndsWith("MB"))
                    bytesY = (long)numY * 1048576;
                else if (strY.EndsWith("GB"))
                    bytesY = (long)numY * 1073741824;
                else
                    bytesY = (long) numY;
    
                var compareResult = bytesX.CompareTo(bytesY);
    
                if (_orderOfSort == SortOrder.Ascending)
                {
                    // Ascending sort is selected, return normal result of compare operation
                    return compareResult;
                }
                if (_orderOfSort == SortOrder.Descending)
                {
                    // Descending sort is selected, return negative result of compare operation
                    return (-compareResult);
                }
                // Return '0' to indicate they are equal
                return 0;
            }
    
            /// 
            /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
            /// 
            public int SortColumn
            {
                set
                {
                    _columnToSort = value;
                }
                get
                {
                    return _columnToSort;
                }
            }
    
            /// 
            /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
            /// 
            public SortOrder Order
            {
                set
                {
                    _orderOfSort = value;
                }
                get
                {
                    return _orderOfSort;
                }
            }
        }
    }
    

    Change namespace name with yours.

    Add following line in your form.

    private readonly ListViewSizeSorter _listViewSizeSorter;
            public Form1()
            {
                InitializeComponent();
                _listViewSizeSorter = new ListViewSizeSorter {SortColumn = 1};
            }
    
    private void ListViewFilesColumnClick(object sender, ColumnClickEventArgs e)
            {
                // Determine if clicked column is already the column that is being sorted.
                if (e.Column == _listViewColumnSorter.SortColumn)
                {
                    if(e.Column.Equals(1))
                    {
                        listViewFiles.ListViewItemSorter = _listViewSizeSorter;
                        // Reverse the current sort direction for this column.
                        _listViewSizeSorter.Order = _listViewSizeSorter.Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
                    }
                }
                else
                {
                    // Set the column number that is to be sorted; default to ascending.
                    _listViewColumnSorter.SortColumn = e.Column;
                    _listViewColumnSorter.Order = SortOrder.Ascending;
                }
    
                // Perform the sort with these new sort options.
                listViewFiles.Sort();
            }
    

提交回复
热议问题