Listview subitem sorting..

  • Thread starter Thread starter Per Rollvang
  • Start date Start date
P

Per Rollvang

Hi All!

Why have subitem sorting been removed? Or maybe not? Am I missing something?

Anyone?
 
Hi Vanessa!

I'm trying to implement sorting of listview-items, by other columns than the
first...

In VisualStudio 6.0 (the old common controls) this was as easty as to set a
property, but in .NET, this seem to be a big engineering project. Or am I
missing something...

-Per

Vanessa said:
Your question is not so clear.
 
I had the same problem as you. Now there must be a better way,
however, following is guaranteed to work:

I have posted a sort routine to Robir.
There you will see a swap somewhere in the middle.
If you transfer the listView colums to individual arrays
and if you sort the value in which sequence you want the output
then simultaneously swap the other array elements
(by adding a swap line to the routine per additional column == aray)
then afterwards you list the results in the listView
and it will be sorted.

If you change the sign around,
sorting will be the other way.


Per Rollvang said:
Hi Vanessa!

I'm trying to implement sorting of listview-items, by other columns than the
first...

In VisualStudio 6.0 (the old common controls) this was as easty as to set a
property, but in .NET, this seem to be a big engineering project. Or am I
missing something...

-Per
 
Yes you can sort by different columns, you have to create a class that’s derived from IComparer. Check your documentation for the ListView's class "Sort method", not the property, for a good example

A crude example

// call to immediately sor
listView.ListViewItemSorter = new ComparerClass(columnNumber)

// IComparer Clas
class ComparerClass: ICompare

private int col

public ComparerClass (int column) { col = column;

public int Compare(object x, object y

// cast items to string
return string.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text)



You can add constructors to inform the sort whether its going to be strings, ints, and other stuff
 
Vas said:
Yes you can sort by different columns, you have to create a class that's
derived from IComparer. Check your documentation for the ListView's class
"Sort method", not the property, for a good example.
A crude example:

// call to immediately sort
listView.ListViewItemSorter = new ComparerClass(columnNumber);


// IComparer Class
class ComparerClass: IComparer
{
private int col;

public ComparerClass (int column) { col = column; }

public int Compare(object x, object y)
{
// cast items to strings
return string.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}


You can add constructors to inform the sort whether its going to be
strings, ints, and other stuff.Thanks a lot Vas!

I really got pushed in the right direction! If I can sit back and read a
bit, I may figure out how to sort numerics as well... ; )

-Per
 
Use Parse to change the strin

long a = long.Parse(((ListViewItem)x).SubItems[col].Text)
long b = long.Parse(((ListViewItem)y).SubItems[col].Text)

and use a conditional statemen

(a < b) ? -1 : ((a > b) ? 1 : 0);

If you multiply the result of any of the tests by -1 you can sort ascending/descending

Also test for white spaces, return 1 for x and -1 for y to keep the spaces down and out the way, I think.
 
Back
Top