listbox numeric sort problem

I’m having a little trouble getting my 67 year old brain around how to make this work.

I have a listbox populated from a table of delivery addresses. The 1st column is the sequence of the drop-off which needs to be user editable. This all works well so far. But after the cell is edited, I need to reset the sequences of all the addresses and resort the rows in the listbox.

I have the sequence column right justified so the numbers look like:

7
8
9
10

it sorts 1, 10, 11, 2 and from there correctly (there are 11 rows in the table)

Thinking this should sort properly but it doesn’t. I have studied the CompareRows method and can’t quite see how to make it work.

Xojo 2018r2, Windows 10 (via Parallels on a Mac Pro - Sierra.

There’s an example in the language reference:

http://documentation.xojo.com/index.php/ListBox.CompareRows

Hi Bill:

The Sorts entry in the documentation talks about that. You have to write code to do the sort for you.

If this forum softwaer worked better, I would advice to do a search cause this was already answered here. But…

Someone have the link to this answer ?

I zero filled the numbers (001, 002…010, 011) and it works correctly but is this the only way?

Jason, I did read that but, like I said, just having some trouble seeing how to make the process work.

Emile, I did some searching and just didn’t find anything exactly on point.

I do not know why I have two times the same answer, sorry.

This is the way to go. Try searching the forum with this word (and you will also get an answer for sorting entries characters e, é, è, etc. in the “correct” way.

That is what I wrote: the search feature is bad. I will try…

This one ?
https://forum.xojo.com/32047-how-to-programatically-execute-a-listbox-sort-using-comparerows

or this one
https://forum.xojo.com/41697-order-listbox-avec-chiffre (with Translate Google…)

I tried a search with only CompareRow.

A last one:
https://forum.xojo.com/47031-listbox-alpha-numerical-sort

I find the examples on the xojo site a bit confusing. If you only have one column with the numbers in it, then use this code in your Listbox’s CompareRows event:

dim v1 as Double = me.Cell (row1, column).Val dim v2 as Double = me.Cell (row2, column).Val result = Min (1, Max (-1, v1 - v2)) return true

I think the examples use a Select Case just to not use this type of sorting on every column.
That code may not work for sorting regular strings.

No offense, Thomas, but your example is even more obscure than the one in the documentation. I do give you points for compact code, though.

The thing you want to do is:
Isolate just the one column you want to do a special sort on
Set the value of result to:
-1 if row1 < row2
0 if row1 = row2
1 if row1 > row2

Something like

dim v1, v2 as integer

// Isolate the column we want to do a special sort, eg., column 1
if Column = 1 then
   // convert the value of each row from string to integer
   v1 = val(me.Cell(row1, column))
   v2 = val(me.Cell(row2, column))

   // compare the integer values and set result
   if v1 < v2 then result = -1
   if v1 = v2 then result = 0
   if v1 > v2 then result = 1

   // remember to return True so the system will know you're doing the sort yourself
   return True
end if

Tim, will it make any difference (speed, memory use) if I dim v1 and v2 inside the if - end if?

I see you don’t supply Return False and your code works with my test. Do you know why the examples always say to Return False for other columns?

Return False is just being explicit about the return value. If you don’t return a value from a method with a return value it will use the default for that data type. The default for Boolean is false. (String is “” (empty), Integer is 0, classes nil, and so on)

Perhaps a few microseconds at most, and +/- 8 bytes of memory. So for all practical purposes, it makes no difference.

[quote=404427:@Tim Hare] // compare the integer values and set result
if v1 < v2 then result = -1
if v1 = v2 then result = 0
if v1 > v2 then result = 1[/quote]
You can further simplify this to:

result = sign(v1 - v2)

Right. But it doesn’t help the uninitiated understand. I’m all for tight code, except when you’re trying to be very explicit and explain what’s going on.

I had a few extra cups of coffee and dug into the CompareRows and made it work .

thanks everyone.

Well… when XOJO told me that an infinite loop on a for / next (downto from 3 to 0) it’s normal to keep looping forever…

You can expect anything from XOJO!

[quote=404749:@Alexandre Cunha]Well… when XOJO told me that an infinite loop on a for / next (downto from 3 to 0) it’s normal to keep looping forever…

You can expect anything from XOJO![/quote]
Care to explain?

For i=3 to 0 // never executes
For i=3 downto 0 // steps 3,2,1, 0
For i=0 downto 3 // never executes
For i=0 to 3 // steps 0,1,2,3

but no combination gives an infinite loop