SoFunction
Updated on 2025-04-07

Listview control implements clicking on the list header to perform listview sorting example sharing


using System;
using ;
using ;
namespace Common
{
    /// <summary>
/// Automatic sorting function of clicking column title for ListView
    /// </summary>
    public class ListViewHelper
    {
        /// <summary>
/// Constructor
        /// </summary>
        public ListViewHelper()
        {
            //
// TODO: Add constructor logic here
            //
        }
        public static void ListView_ColumnClick(object sender, e)
        {
            lv = sender as ;
// Check whether the clicked column is the current sorted column.
            if ( == ( as ListViewColumnSorter).SortColumn)
            {
// Reset the sorting method of this column.
                if (( as ListViewColumnSorter).Order == )
                {
                    ( as ListViewColumnSorter).Order = ;
                }
                else
                {
                    ( as ListViewColumnSorter).Order = ;
                }
            }
            else
            {
// Set the sorting sequence, default to forward sorting
                ( as ListViewColumnSorter).SortColumn = ;
                ( as ListViewColumnSorter).Order = ;
            }
// Sort ListView with a new sorting method
            (()sender).Sort();
        }
    }
    /// <summary>
/// Inherited from IComparer
    /// </summary>
    public class ListViewColumnSorter :
    {
        /// <summary>
/// Specify which column to sort by
        /// </summary>
        private int ColumnToSort;
        /// <summary>
/// Specify the sorting method
        /// </summary>
        private OrderOfSort;
        /// <summary>
/// Declare CaseInsensitiveComparer class object
        /// </summary>
        private ObjectCompare;
        /// <summary>
/// Constructor
        /// </summary>
        public ListViewColumnSorter()
        {
// Sort by the first column by default
            ColumnToSort = 0;
// The sorting method is not sorted
            OrderOfSort = ;
// Initialize the CaseInsensitiveComparer class object
            ObjectCompare = new ();
        }
        /// <summary>
///Rewrite the IComparer interface.
        /// </summary>
/// <param name="x">The first object to be compared</param>
/// <param name="y">The second object to compare</param>
/// <returns>Result of comparison. If equal, return 0, return 1 if x is greater than y, return -1 if x is less than y</returns>
        public int Compare(object x, object y)
        {
            int compareResult;
            listviewX, listviewY;
// Convert the comparison object to a ListViewItem object
            listviewX = ()x;
            listviewY = ()y;
            string xText = [ColumnToSort].Text;
            string yText = [ColumnToSort].Text;
            int xInt, yInt;
// Compare, if the value is an IP address, sort according to the rules of the IP address.
            if (IsIP(xText) && IsIP(yText))
            {
                compareResult = CompareIp(xText, yText);
            }
else if ((xText, out xInt) && (yText, out yInt)) //Is it all numbers
            {
//Compare numbers
                compareResult = CompareInt(xInt, yInt);
            }
            else
            {
//Comparison object
                compareResult = (xText, yText);
            }
//Return the correct comparison result based on the above comparison result
            if (OrderOfSort == )
            {
// Because it is sorted in positive order, the result is returned directly
                return compareResult;
            }
            else if (OrderOfSort == )
            {
// If it is in reverse order, you must take a negative value and return it
                return (-compareResult);
            }
            else
            {
// If equal, return 0
                return 0;
            }
        }
        /// <summary>
/// Determine whether it is the correct IP address, IP range (0.0.0.0~255.255.255)
        /// </summary>
/// <param name="ip">IP address to be verified</param>
        /// <returns></returns>
        public bool IsIP(String ip)
        {
            return (ip, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$").Success;
        }
        /// <summary>
/// Compare the size of two numbers
        /// </summary>
/// <param name="ipx">The first object to be compared</param>
/// <param name="ipy">The second object to compare</param>
/// <returns>Result of comparison. If equal, return 0, return 1 if x is greater than y, return -1 if x is less than y</returns>
        private int CompareInt(int x, int y)
        {
            if (x > y)
            {
                return 1;
            }
            else if (x < y)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
/// Compare the size of two IP addresses
        /// </summary>
/// <param name="ipx">The first object to be compared</param>
/// <param name="ipy">The second object to compare</param>
/// <returns>Result of comparison. If equal, return 0, return 1 if x is greater than y, return -1 if x is less than y</returns>
        private int CompareIp(string ipx, string ipy)
        {
            string[] ipxs = ('.');
            string[] ipys = ('.');
            for (int i = 0; i < 4; i++)
            {
                if (Convert.ToInt32(ipxs[i]) > Convert.ToInt32(ipys[i]))
                {
                    return 1;
                }
                else if (Convert.ToInt32(ipxs[i]) < Convert.ToInt32(ipys[i]))
                {
                    return -1;
                }
                else
                {
                    continue;
                }
            }
            return 0;
        }
        /// <summary>
/// Get or set which column to sort by.
        /// </summary>
        public int SortColumn
        {
            set
            {
                ColumnToSort = value;
            }
            get
            {
                return ColumnToSort;
            }
        }
        /// <summary>
/// Get or set the sorting method.
        /// </summary>
        public Order
        {
            set
            {
                OrderOfSort = value;
            }
            get
            {
                return OrderOfSort;
            }
        }
    }
}