zoukankan      html  css  js  c++  java
  • ListView 日期,数字,字符排序

      1private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
      2        {
      3            int sortColumn = e.Column;
      4            ListView listView = (ListView)sender;
      5
      6            if (listView.Name == "listView2")
      7            {
      8                if (listView2.Sorting == SortOrder.Ascending)
      9                    listView2.Sorting = SortOrder.Descending;
     10                else
     11                    listView2.Sorting = SortOrder.Ascending;
     12
     13                this.listView2.ListViewItemSorter = new ListViewItemComparer(e.Column, listView2.Sorting);
     14            }

     15            else if (listView.Name == "listView3")
     16            {
     17                if (listView3.Sorting == SortOrder.Ascending)
     18                    listView3.Sorting = SortOrder.Descending;
     19                else
     20                    listView3.Sorting = SortOrder.Ascending;
     21
     22                this.listView3.ListViewItemSorter = new ListViewItemComparer(e.Column, listView3.Sorting);
     23            }

     24            else if (listView.Name == "listView4")
     25            {
     26                if (listView4.Sorting == SortOrder.Ascending)
     27                    listView4.Sorting = SortOrder.Descending;
     28                else
     29                    listView4.Sorting = SortOrder.Ascending;
     30
     31                this.listView4.ListViewItemSorter = new ListViewItemComparer(e.Column, listView4.Sorting);
     32            }

     33            else if (listView.Name == "listView5")
     34            {
     35                if (listView5.Sorting == SortOrder.Ascending)
     36                    listView5.Sorting = SortOrder.Descending;
     37                else
     38                    listView5.Sorting = SortOrder.Ascending;
     39
     40                this.listView5.ListViewItemSorter = new ListViewItemComparer(e.Column, listView5.Sorting);
     41            }

     42        }

     43
     44        class ListViewItemComparer : System.Collections.IComparer
     45        {
     46            private int col;
     47            private SortOrder order;
     48            public ListViewItemComparer()
     49            {
     50                col = 0;
     51                order = SortOrder.Ascending;
     52            }

     53            public ListViewItemComparer(int column, SortOrder order)
     54            {
     55                col = column;
     56                this.order = order;
     57            }

     58            public int Compare(object x, object y)
     59            {
     60                int returnVal;
     61                decimal w;
     62                DateTime r;
     63                bool k = Decimal.TryParse(((ListViewItem)x).SubItems[col].Text, out w);
     64                bool s = DateTime.TryParse(((ListViewItem)x).SubItems[col].Text, out r);
     65                if (k == true && order != SortOrder.Descending)//数字正排序
     66                {
     67                    Decimal k1 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
     68                    Decimal k2 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
     69                    if (k2 > k1)
     70                        returnVal = -1;
     71                    if (k2 < k1)
     72                        returnVal = 1;
     73                    else
     74                        returnVal = 0;
     75                }

     76                else
     77                {
     78                    if (s == true)//时间排序
     79                    {
     80                        System.DateTime firstDate = DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
     81                        System.DateTime secondDate = DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
     82                        returnVal = DateTime.Compare(firstDate, secondDate);
     83                    }

     84                    else//字符排序
     85                    {
     86                        returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
     87                                    ((ListViewItem)y).SubItems[col].Text);
     88                    }

     89                }

     90
     91                if (order == SortOrder.Descending)
     92                {
     93                    if (k == true)//数字倒排序
     94                    {
     95                        System.Int32 k3 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
     96                        System.Int32 k4 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
     97                        if (k3 > k4)
     98                            returnVal = 1;
     99                        if (k3 < k4)
    100                            returnVal = -1;
    101                        else
    102                            returnVal = 0;
    103                    }

    104                    returnVal *= -1;
    105                }

    106                return returnVal;
    107            }

    108        }
  • 相关阅读:
    《C#高级编程》读书笔记(十五):任务、线程和同步之二 任务
    sklearn训练模型的保存与加载
    机器学习中样本不平衡的处理方法
    剑指Offer(四):重建二叉树
    剑指Offer(三):从尾到头打印链表
    机器学习笔记(一)----基本概念
    100 个网络基础知识普及,看完成半个网络高手
    协方差基本概念及公式
    正态分布基本概念及公式
    np.random.multivariate_normal方法浅析
  • 原文地址:https://www.cnblogs.com/duoe/p/1097420.html
Copyright © 2011-2022 走看看