[Gambas-user] ListBox numeric sorting

Benoit Minisini gambas at ...1...
Sun Jul 15 11:08:25 CEST 2007


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




More information about the User mailing list