ListView Sorting negative and positive numbers

  • Thread starter Thread starter David Hubball
  • Start date Start date
D

David Hubball

Hi

Does anyone know why when using the ListBox to sort these numbers (in
descending order) for example :-
5.00
-2.50
2.50
0.00

the Listbox displays -.2.50 above 2.50.
Would anyone know how to get it to display in numerical order like :-
5.00
2.50
0.00
-2.50

Much Appreciated if anyone know how to get the Sorting correct for
mre.

Kind Regards
David
 
Hi Peter

I'm just adding string literals to the ListView control like as
below :-

ListViewItem objitem;
listViewPatient.Items.Clear();

objitem = listViewPatient.Items.Add("5.00");
objitem = listViewPatient.Items.Add("2.50");
objitem = listViewPatient.Items.Add("-2.50");
objitem = listViewPatient.Items.Add("0.00");

Would you still be able to say why the Sorted Property still does not
show in strict descending numerical order. I think that ListView will
only accept strings anyway as far as I can tell.

Thanks

David
 
David Hubball said:
Hi Peter

I'm just adding string literals to the ListView control like as
below :-

ListViewItem objitem;
listViewPatient.Items.Clear();

objitem = listViewPatient.Items.Add("5.00");
objitem = listViewPatient.Items.Add("2.50");
objitem = listViewPatient.Items.Add("-2.50");
objitem = listViewPatient.Items.Add("0.00");

Would you still be able to say why the Sorted Property still does not
show in strict descending numerical order. I think that ListView will
only accept strings anyway as far as I can tell.

The items are added as strings so they will predictably be sorted
lexicographically. Like you say it seems that ListView only accepts string,
rather than objects so you will have to sort the items numerically yourself
then add the results of your sorting to the ListView, with sorting turned
off obviously.

Sorting is a trivial task if you're using Linq, simply generate a query that
sorts by a given field - otherwise you can write a simple comparer for older
versions of C#.
 
Are you using ListBox or ListView?  Your second post is not consistent  
with the first, and ListView doesn't have a Sorted property.

In any case, there's nothing about the above code example that suggests to  
me you should get the order you stated in your first post.  Items addedas  
strings will, as the person posting as "JY" says, be sorted without regard  
to the actual numeric value.  But I would expect a string starting with 
"-" to wind up at one end of the sort or the other (depending on ascending  
or descending), not in the middle as your first post says.

The exact answer to your question will depend on what class you're  
actually using.  ListView has slightly better sort support than ListBox..  
In either case, your main issue will be ensuring that you are sorting on  
the actual numeric value, rather than the string value.

Pete- Hide quoted text -

- Show quoted text -

Sorry for any confusion - I'm just focusing on ListView which does
have a Sort Property in VS 2005 and VSExpress2008. ListBox has a
Sorted property which can only be set to True of False so the options
are more limited.

Cheers
David
 
Probably an artefact from Win 32 where ' and - are 'invisible' characters
when strings are involved in a sort (à la Win 32).

If so, "A-B" will be sorted to fit between "AA" and "AC" .

Maybe you can find a signature (optional argument with Framework 4.0) or
something similar to specify which method of sorting you want (ex. case
sensistive or not, accent sensitive or not, ... ) which does NOT consider
that - and ' are invisible characters.


Vanderghast, Access MVP
 
Back
Top