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.
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.
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
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
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)
[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:
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.