[Gambas-user] ListBox numeric sorting

timothy timothy.marshal-nichols at ...247...
Sun Jul 15 12:11:27 CEST 2007


> -----Original Message-----
> From: gambas-user-bounces at lists.sourceforge.net [mailto:gambas-user-
> bounces at lists.sourceforge.net] On Behalf Of Benoit Minisini
> Sent: Sunday, 15 July 2007 10:08 AM
> To: mailing list for gambas users
> Subject: Re: [Gambas-user] ListBox numeric sorting
> 
> On dimanche 15 juillet 2007, timothy wrote:
> > Hello,
> >
> > I wanted to sort some numbers in a ListBox by setting the Sorted
> > property to true. Alas I am having some trouble getting usable
results.
> >
> > Example 1:
> > Take the numbers: "1", "45", "234", "1234"
> > This will be sorted (correctly) in the ListBox according to the
ASCII
> > sort order. But it is not that useful when you want to display a
long
> > list of numbers.
> >
> > Example 2:
> > Take the numbers: "0001", "0045", "0234", "1234"
> > This will be sorted (correctly) according to the ASCII sort order.
> > Numbers will be in the correct order. But it is not very readable.
> >
> > Example 3:
> > Take the numbers: "   1", "  45", " 234", "1234"
> > This is the one I want. Numbers have been padded with spaces. But it
> > gives the same sort order as example 1!!! They are NOT in the
correct
> > ASCII sort order! The spaces appear to have been removed before
sorting.
> >
> > Example 4:
> > Take the numbers: "...1", "..45", ".234", "1234"
> > Oddly gives the same sort order as example 1. The same goes for any
> > other non alphanumeric characters e.g.: /#&.=
> >
> > Example 5:
> > Take the numbers: "AAA1", "AA45", "A234", "1234"
> > This gives the correct ASCII sort order. (Obviously not in the
numeric
> > sort order.) But, of course, it is not very useful.
> >
> > It's almost as if all non alphanumeric characters are removed from
the
> > strings before sorting. This makes it almost impossible to sort
numbers
> > readably. And it can give odd results when sorting other strings in
a
> > ListBox.
> >
> > Also I have had the same problems with the ColumnView.
> >
> > Thanks
> >
> > 8-{)} Timothy Marshal-Nichols
> > <mailto: timothy.marshal-nichols at ...247...>
> >
> 
> Sorting is done by a QT algorithm, and I think it is suitable only for
> alphabetic strings.
> 
> In other words, you must do it by hand.
> 
> You have two solutions:
> 
> 1) Sort your integers before inserting them in a unsorted ListBox.
> 
> 2) Use a ColumnView with Sorted = True and its Compare event, that is
> raised
> each time the ColumnView needs to compare two elements, so that you
can
> override the default sort.
> 
> Regards,
> 
> --
> Benoit Minisini
> 

Thanks. That was an amazingly fast reply. I did not know about the
Compare event on the ColumnView.

I have ended up using a ColumnView with a single column. Setting the
Sorted property to True and adding this event: 

PUBLIC SUB ColumnViewPorts_Compare(Key AS String, OtherKey AS String)
  DIM Key1 AS Integer
  DIM Key2 AS Integer
  Key1 = CInt(Key)
  Key2 = CInt(OtherKey)
  IF Key1 > Key2 THEN 
    ColumnViewPorts.Compare = If(ColumnViewPorts.Columns.Ascending, 1,
-1)
  ELSE IF Key1 < Key2 THEN 
    ColumnViewPorts.Compare = If(ColumnViewPorts.Columns.Ascending, -1,
1)
  ELSE 
    ColumnViewPorts.Compare = 0
  END IF
END

This works fine (so long as my one column only has numbers in it). And
you do not need any padding on the numbers. The If(...) is needed handle
the ascending and descending sort order.

Thanks

8-{)} Timothy Marshal-Nichols
<mailto: timothy.marshal-nichols at ...247...>








More information about the User mailing list