Solved

Sorting a ListView or TreeView by number and text

Posted on 2004-09-14
3
285 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 300 total points
ID: 12061500
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 200 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Build JSON from table records 17 41
Using MS Code on my Mac 6 47
How do I get the id from URL? 19 48
cs.Designer Issue(2) 2 23
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now