?
Solved

Sorting a ListView or TreeView by number and text

Posted on 2004-09-14
3
Medium Priority
?
313 Views
Last Modified: 2008-02-01
 I've for a listview and a treeview control on a form that I want to sort by number and text.  For example, it should sort this way: 1, 2, 3, 9, 10, 11, 99, 100, 101, Alpha, Beta, etc...  instead of 1, 10, 100, 101, 11, 2, 3, 9, Alpha, Beta, etc.  How can I solve this problem in both controls?

  Also, I'd like to be able to sort the TreeView by group/detail (the way that windows sorts folders).  How can I implement this?

  This is something I'd like to get answered quickly, so 500 points.  Thanks!

Gavin
0
Comment
Question by:gmurphy79
3 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 900 total points
ID: 12061500
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 600 total points
ID: 12061584
add column click event by

private void ColumnClick(object o, ColumnClickEventArgs e){
           // Set the ListViewItemSorter property to a new ListViewItemComparer object.
         this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
           // Call the sort method to manually sort the column based on the ListViewItemComparer implementation.
         listView1.Sort();
      }

or you just want call sort by yourself

this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
listView1.Sort();

write a ItemCompareer class to implement IComparer

class ListViewItemComparer : IComparer {
      private int col;
      public ListViewItemComparer() {
          col=0;
      }
      public ListViewItemComparer(int column) {
          col=column;
      }
      public int Compare(object x, object y)
      {
          string valuex,valuey;
          int res;

          valuex=(((ListViewItem)x).SubItems[col].Text;
          valuey=(((ListViewItem)y).SubItems[col].Text;

          if(IsNum(valuex))
          {
               if(IsNum(valuey))
                    res=int.Parse(valuex).CompareTo(int.Parse(valuey));
              else
                    res=-1;
          }
         else
         {
               if(isNum(valuey))
                     res=1;
               else
                    res=valux.CompareTo(valuey);
          }
          return res;
      }
   }

0
 

Author Comment

by:gmurphy79
ID: 12084569
Thanks both of you.  It looks like I need to write a class that implements IComparer and then assign it to the listviewitemsorter.  I assume I can do the same for a treeview (although I haven't had time to do this coding yet).  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question