?
Solved

Sorting a listview column with numbers and blanks

Posted on 2006-05-31
10
Medium Priority
?
352 Views
Last Modified: 2010-04-16
Hi... I have a listview column that will either be blank or have a number in it.  I want the numbers to be at the top of the column with the blanks at the bottom (in any order, I don't care)... I have the Icomparer working, but the blanks come out on top.  Can someone show me the light on this one?
0
Comment
Question by:rmmarsh
  • 6
  • 4
10 Comments
 
LVL 10

Expert Comment

by:dkloeck
ID: 16801093
Sorting in ListViews is done by creating a sorter class (implements IComparer) that takes care of determening íf one item is "larger" than another item. The items could be text, dates, icons, or whatever you put in your listview.

When you've created your sorter class, you set the ListViewItemSorter property of the listview to an instance of your sorter. Then, when you call the Sort method on the listview (for example when someone clicks the header), your sorter class will be called from the listview (as many times as there are items) to determine how the items should be sorted.

Check out the MSDN info about this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistviewclasslistviewitemsortertopic.asp

Here's another article about that:
http://www.c-sharpcorner.com/Code/2002/May/MultiColumnListViewSorting.asp
0
 

Author Comment

by:rmmarsh
ID: 16801179
I have Icomparer working... the problem is that I want to "ignore" any column that has a blank in it since in the collating sequence, blanks are the lowest, with any text following.
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 16801210
why don't you sort the blanks with your IComparer?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rmmarsh
ID: 16801323
I do and they come out on top... I am sorting on one column...I want the numbers (which are text also) to appear first, followed by the blank subitems in that column.  

It's sort of like having a race and I want the first place entrant to appear first among all of the entrants, followed by the second place, etc... with those that have not come in yet to appear in any order...
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 16801352
can you show me the code of your icomparer?
0
 

Author Comment

by:rmmarsh
ID: 16801370
   class ListViewItemComparer : IComparer
    {
        private int col;

        //----------------------------------------------------------------
        public ListViewItemComparer()
        {
            col = 0;
        }

        //----------------------------------------------------------------
        public ListViewItemComparer(int column)
        {
            col = column;
        }

        //----------------------------------------------------------------
        public int Compare(object x, object y)
        {
            return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
        }
    }
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 16801465
change your Compare(object x, object y) method as follows

 public int Compare(object x, object y)
        {
            if (x == "")
            {
                return 1;
            }
            else if (y == "")
            {
                return -1;
            }
            else
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
        }
0
 
LVL 10

Expert Comment

by:dkloeck
ID: 16801478
oh..sorry, instead of x==""

you must write
((ListViewItem)x).SubItems[col].Text==0

same for y
0
 
LVL 10

Accepted Solution

by:
dkloeck earned 2000 total points
ID: 16801483
d'oh!

i meant ((ListViewItem)x).SubItems[col].Text==""
0
 

Author Comment

by:rmmarsh
ID: 16801736
That's it!  thank you so very much... (I was close with my experimenting, but I think I left out the ==, which is a common mistake I make)...
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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month15 days, 6 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