Solved

Sorting a ListView or TreeView by number and text

Posted on 2004-09-14
3
301 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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