listview sort on integer question 
Author Message
 listview sort on integer question

I have a listview box set to report view with 4 columns.  The first
column
has integers in them (from 0-19).  Why is it that when I set the .SORTED
= true
and the .SORTKEY to the column, it sorts in this fashion:

0
1
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6

what the?!?!?!?  How can I get it to sort numerically in ascending
order?

Any help would be appreciated.

ron

--
Ron Wierckx, C.E.T
Digital Systems Technologist
RTDS Technologies Inc.
200-1619 Pembina Highway
Winnipeg, MB, Canada R3T-3Y6
ph:(204)989-9713 fx:(204)452-4303



Tue, 13 Nov 2001 03:00:00 GMT  
 listview sort on integer question
It's sorting the column as strings, not integers. Just as 'ab' comes
before 'b' alphabetically, '10' comes before '2'. One way to fix this
would be to use leading 0's when you're populating the list. The values
would then sort as: '01', '02', '03', ..., '10', '11', etc.

   HTH,
    -Tom.


Quote:

> I have a listview box set to report view with 4 columns.  The first
> column
> has integers in them (from 0-19).  Why is it that when I set the
.SORTED
> = true
> and the .SORTKEY to the column, it sorts in this fashion:

> 0
> 1
> 10
> 11
> 12
> 13
> 14
> 15
> 16
> 17
> 18
> 19
> 2
> 3
> 4
> 5
> 6

> what the?!?!?!?  How can I get it to sort numerically in ascending
> order?

> Any help would be appreciated.

> ron

> --
> Ron Wierckx, C.E.T
> Digital Systems Technologist
> RTDS Technologies Inc.
> 200-1619 Pembina Highway
> Winnipeg, MB, Canada R3T-3Y6
> ph:(204)989-9713 fx:(204)452-4303


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


Tue, 13 Nov 2001 03:00:00 GMT  
 listview sort on integer question
What the listbox is doing on 'sorted' is sorting them into alphabetical
order not numerical.

Instead of using 1,2,3 - 9 try and use 01,02,03,04 etc...

This should work.



Tue, 13 Nov 2001 03:00:00 GMT  
 listview sort on integer question

Quote:
>has integers in them (from 0-19).  Why is it that when I set the .SORTED
>= true
>and the .SORTKEY to the column, it sorts in this fashion:

>0
>1
>10

Because all items in the listview are strings, so it is sorting the strings,
not the number.  You'll have the same problem with dates.  There are various
ways around this.  The easiest is to add a "hidden" column by setting that
columns width to zero, then, if the user clicks on the header of the numeric
column, run through all items and load the "hidden" column with a sortable
string value, something like
format$(.listitems(index).subitems(3),"0000000000") or whatever, then, sort
the list by the "hidden" column programmatically.  The reason I keep putting
quotes around "hidden" is because the user can resize the column and thus it
is not hidden any more.  To stop them from doing this, you need to subclass
the listview and intercept resize and double click events.  I have code to
do this if you are interested.

I've also seen another solution (also using subclassing) that writes a
custom "compare" subroutine to compare the list values and return the
highest one.  (I think I got it from the knowledge base).  The problem with
this is that in the end, the list "appears" sorted to the user, but if you
iterate through the items programmatically, you get them in the original
order.  Since both methods involve subclassing,  I prefer the first method.



Sat, 17 Nov 2001 03:00:00 GMT  
 listview sort on integer question

Quote:

> I have a listview box set to report view with 4 columns.  The first
> column
> has integers in them (from 0-19).  Why is it that when I set the .SORTED
> = true
> and the .SORTKEY to the column, it sorts in this fashion:

> 0
> 1
> 10
> 11
> 12
> 13
> 14
> 15
> 16
> 17
> 18
> 19
> 2
> 3
> 4
> 5
> 6

> what the?!?!?!?  How can I get it to sort numerically in ascending
> order?

> Any help would be appreciated.

> ron

A simpler way would be to a) sort the items in an array (using any
sorting routine you like) b) add them in an unsorted list (using API
would be much faster for a number of items larger than 50-100).


Sun, 25 Nov 2001 03:00:00 GMT  
 listview sort on integer question

Quote:


>> I have a listview box set to report view with 4 columns.  The first
>> column
>> has integers in them (from 0-19).  Why is it that when I set the .SORTED
>> = true
>> and the .SORTKEY to the column, it sorts in this fashion:

>> 0
>> 1
>> 10
>> 11
>> 12
>> 13
>> 14
>> 15
>> 16
>> 17
>> 18
>> 19
>> 2
>> 3
>> 4
>> 5
>> 6

>> what the?!?!?!?  How can I get it to sort numerically in ascending
>> order?

>> Any help would be appreciated.

>> ron

>A simpler way would be to a) sort the items in an array (using any
>sorting routine you like) b) add them in an unsorted list (using API
>would be much faster for a number of items larger than 50-100).

When you add the numbers to the listview (i guess either as text or
subitem)  pad the numbers with spaces.  This will get the listview
to sort correctly.

Ex.  Instead of
1
11
122

pad with spaces
  1
 11
122



Mon, 26 Nov 2001 03:00:00 GMT  
 listview sort on integer question
The problem here is that the control sorts according to the Ascii value of
each character.  The trick is to sort the string according to its value, not
its character representation.  But in another post, someone pointed out that
padding it with spaces works.  Indeed it does, however you should pad each
string according to the longest string in your list.  Thus you'll have to
write code to determine the longest string.  Simple enough.  I call it the
1,100,2 test.


Quote:

> > I have a listview box set to report view with 4 columns.  The first
> > column
> > has integers in them (from 0-19).  Why is it that when I set the .SORTED
> > = true
> > and the .SORTKEY to the column, it sorts in this fashion:

> > 0
> > 1
> > 10
> > 11
> > 12
> > 13
> > 14
> > 15
> > 16
> > 17
> > 18
> > 19
> > 2
> > 3
> > 4
> > 5
> > 6

> > what the?!?!?!?  How can I get it to sort numerically in ascending
> > order?

> > Any help would be appreciated.

> > ron

> A simpler way would be to a) sort the items in an array (using any
> sorting routine you like) b) add them in an unsorted list (using API
> would be much faster for a number of items larger than 50-100).



Mon, 26 Nov 2001 03:00:00 GMT  
 listview sort on integer question
Well, the columns in a listview are displayed as strings (text) so they are
left aligned and sorted as text.
There is a way to intercept the sort and provide your own comparison
routines, which involve convert the text back to numeric and telling the
system which of two elements is "higher". This works but is woefully slow.
The best bet is to get the data into correct sequence before inserting it
into the listview. Of course if you are using Header click to sort, you will
have to use your own comparison routine. The paper describing this was
addressed at sorting dates, which suffer the same problem, but can be
modified for any data type that is not text. See Q170884.
HTH
--
Gary (MCT, MCPS, MCSD)
http://www.enter.net/~garyl/  for references to good books

Quote:

>The problem here is that the control sorts according to the Ascii value of
>each character.  The trick is to sort the string according to its value,
not
>its character representation.  But in another post, someone pointed out
that
>padding it with spaces works.  Indeed it does, however you should pad each
>string according to the longest string in your list.  Thus you'll have to
>write code to determine the longest string.  Simple enough.  I call it the
>1,100,2 test.




>> > I have a listview box set to report view with 4 columns.  The first
>> > column
>> > has integers in them (from 0-19).  Why is it that when I set the
.SORTED
>> > = true
>> > and the .SORTKEY to the column, it sorts in this fashion:

>> > 0
>> > 1
>> > 10
>> > 11
>> > 12
>> > 13
>> > 14
>> > 15
>> > 16
>> > 17
>> > 18
>> > 19
>> > 2
>> > 3
>> > 4
>> > 5
>> > 6

>> > what the?!?!?!?  How can I get it to sort numerically in ascending
>> > order?

>> > Any help would be appreciated.

>> > ron

>> A simpler way would be to a) sort the items in an array (using any
>> sorting routine you like) b) add them in an unsorted list (using API
>> would be much faster for a number of items larger than 50-100).



Mon, 26 Nov 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Listview sorting integers and doubles?

2. Yet another listview sort by column question.

3. ListView Sort order Question

4. ListView Sorting question...

5. Listview column click to sort question

6. Listview sort question

7. listview sorting question

8. Get items from Listview sorted with API:s in sorted order

9. Sorting integers

10. Sort Array of integers? VB5 Pro Ed.(sp2)

11. How to Sort integer numbers inside listbox

12. TDC, integers sorted as strings

 

 
Powered by phpBB® Forum Software