5 Replies - 1670 Views - Last Post: 15 October 2014 - 11:35 AM

#1 expir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-August 12

C++ .NET Windows Forms App, problem with default sorter in ListViews

Posted 15 October 2014 - 08:46 AM

Hi,
i'm doing a windows form application in C++ .NET and I have a ListView with 2 columns: Number (numeric) and Name (text), what I want to do is to sort all the items basing on the numeric value of the Number column, I have set the 'Sorting' property of the listview to Ascending, and I thought it was working because it was sorting items good, even if some were removed and new were placed in their places. However it works only for numbers 0-9, i'm pretty sure the problem is that the sorter takes the 'Number' column as text and when the number is below 10 it sorts well, but when the number is like 11 it takes the first character and puts it after '1' in the listview.
Here you can see the way I want it
Attached Image
And here you see the problem
Attached Image

So I guess I need some custom way of sorting it, however I have no damn idea how to make it :P
I have a VB.NET class I have found and used a long time ago back when I was using VB, here it is:
Class ListViewItemComparer
    Implements IComparer
    Private col As Integer

    Public Sub New()
        col = 0
    End Sub

    Public Sub New(ByVal column As Integer)
        col = column
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                            Implements System.Collections.IComparer.Compare
        Dim returnVal As Integer = -1
        returnVal = [String].Compare(CType(x,  _
                        ListViewItem).SubItems(col).Text, _
                        CType(y, ListViewItem).SubItems(col).Text)
        Return returnVal
    End Function
End Class

// Usage
      ListView1.Items.Add(lvi)
      Me.ListView1.ListViewItemSorter = New ListViewItemComparer(0)
      ListView1.Sort()

And it worked, however I have no idea how could I convert this into C++, and the fact it's using .NET probably makes it even worse.
Could you link me some class like that one for C++ .NET or some other help ? :P
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ .NET Windows Forms App, problem with default sorter in ListViews

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13566
  • View blog
  • Posts: 54,122
  • Joined: 12-June 08

Re: C++ .NET Windows Forms App, problem with default sorter in ListViews

Posted 15 October 2014 - 08:49 AM

If the number column is being sorted as a string, why are you not putting the 'number' column type as integer?
Was This Post Helpful? 0
  • +
  • -

#3 expir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-August 12

Re: C++ .NET Windows Forms App, problem with default sorter in ListViews

Posted 15 October 2014 - 10:26 AM

And what do you mean by that ? I cannot find anything about data types in listview proporties/columns proporties (maybe because im using Express version of VS ?)
Anyway, I have found out the VB sorter I have found is just a snippet from MSDN, thus there is also a version for C++:
http://msdn.microsof...#code-snippet-2

ref class ListViewItemComparer : public IComparer
{
private:
	int col;

public:
	ListViewItemComparer()
	{
		col = 0;
	}

	ListViewItemComparer(int column)
	{
		col = column;
	}

	virtual int Compare(Object^ x, Object^ y)
	{
		return String::Compare((dynamic_cast<ListViewItem^>(x))->SubItems[col]->Text,
			(dynamic_cast<ListViewItem^>(y))->SubItems[col]->Text);
	}
};


I've added it to my projects namespace and tried to assign it to the listview sorter property like it is in the example:
this->peopleListview->ListViewItemSorter = gcnew ListViewItemComparer(0);

However the compiler spits with a ton of the same errors saying:

Quote

error C2061: syntax error : identifier 'ListViewItemComparer'

Was This Post Helpful? 0
  • +
  • -

#4 expir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-August 12

Re: C++ .NET Windows Forms App, problem with default sorter in ListViews

Posted 15 October 2014 - 11:01 AM

I got around this by placing the ListViewItemComparer class above the form class (idk why but it worked) however this couldn't go through because visual studio form designer requires that the form class is the first one in the file.
This sorter didn't work anyway, so I'm back to the first question: how to sort columns in a ListView by their NUMERIC value ?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13566
  • View blog
  • Posts: 54,122
  • Joined: 12-June 08

Re: C++ .NET Windows Forms App, problem with default sorter in ListViews

Posted 15 October 2014 - 11:22 AM

Is there any particular reason you are using a listview and not, say, a datagrid?
Was This Post Helpful? 0
  • +
  • -

#6 expir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-August 12

Re: C++ .NET Windows Forms App, problem with default sorter in ListViews

Posted 15 October 2014 - 11:35 AM

I've managed to write a numeric comparer basing on examples from other languages, someone may find it useful (you can correct me if I made some mistakes :P)
I've tested it and it works like a charm, at least for me.
ref class ListViewItemComparer : public IComparer
{
private:
	int col;

	public:
		ListViewItemComparer()
		{
			col = 0;
		}

		ListViewItemComparer(int column)
		{
			col = column;
		}

		virtual int Compare(Object^ x, Object^ y)
		{
			int xval, yval;
			xval = int::Parse(dynamic_cast<ListViewItem^>(x)->SubItems[col]->Text);
			yval = int::Parse(dynamic_cast<ListViewItem^>(y)->SubItems[col]->Text);
			// sort ascending
			if (xval > yval) return 1;
			if (yval > xval) return -1;
			return 0;
		}
	};


However I'm still having the problem with syntax error when the class is placed under the form class, and when placed above it works but the designer doesn't work because it requires the form class to be on top.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1