Solved

Sorting a ListView or TreeView by number and text

Posted on 2004-09-14
3
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

730 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