i use sort option for this column but the sort is not good.
I have used ToNumber() and Val() command to convert strings to number.
Any ideas please?
Thanks for your help!


It can understand everything you need. Your callback simply tells the listview how to sort the entries. This allows you to impose any sort logic you want on the listview. Numbers, dates, currencies, whatever.
It's not a RapaGUI bug. As plouf has correctly said, by default all entries are sorted alphabetically and in that case it's correct to put 236550 after 1886087 because 2 comes after 1 in the alphabet. If you want numerical sorting, you need to use a custom callback using Listview.CompareItems to do that. Here's what your example looks like with a custom callback:
Code: Select all
@REQUIRE "RapaGUI"
Function p_CmpFunc(e1, e2)
Local v1, v2 = Val(e1), Val(e2)
If v1 < v2 Then Return(-1)
If v1 > v2 Then Return(1)
Return(0)
EndFunction
moai.CreateApp([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application>
<window title="Test program">
<vgroup>
<listview id="lv" notify="compareitems" forcemode="dataview">
<column sortable="true"/>
</listview>
</vgroup>
</window>
</application>
]])
t = {1289255, 1393876, 1611837, 172877653, 1872262, 1886087, 236550,
2690523, 355653720, 36295634, 4617075, 4848326, 5497692,
61047095, 6256502}
InstallEventHandler({RapaGUI = Function(msg)
If msg.attribute = "CompareItems" Then Return(p_CmpFunc(msg.entry1, msg.entry2))
EndFunction})
For Local k = 0 To ListItems(t) - 1 Do moai.DoMethod("lv", "insert", 0, t[k])
Repeat
WaitEvent
Forever
i can not understand how can this so "smart"airsoftsoftwair wrote: ↑Tue Jan 02, 2024 12:13 pmIt can understand everything you need. Your callback simply tells the listview how to sort the entries. This allows you to impose any sort logic you want on the listview. Numbers, dates, currencies, whatever.
Code: Select all
t = {"21/12/2020","4/5/2021","30/01/1651","12/12/1900"}The p_CmpFunc just gets two values and returns -1, 0, or 1 after it has decided in which order these two values should be. It's up to your own code how the order should be defined.plouf wrote: ↑Tue Jan 02, 2024 2:54 pmi can not understand how can this so "smart"airsoftsoftwair wrote: ↑Tue Jan 02, 2024 12:13 pmIt can understand everything you need. Your callback simply tells the listview how to sort the entries. This allows you to impose any sort logic you want on the listview. Numbers, dates, currencies, whatever.
i try with your example the following
no matter if p_CmpFunc return -1 0 or 1 , date is not correct. do i miss something or is just numbers<>charactersCode: Select all
t = {"21/12/2020","4/5/2021","30/01/1651","12/12/1900"}
Code: Select all
Function p_CmpFuncDate(e1, e2)
Local d1$, m1$, y1$ = PatternFindStrShort(e1, "(%d+)/(%d+)/(%d+)")
Local d2$, m2$, y2$ = PatternFindStrShort(e2, "(%d+)/(%d+)/(%d+)")
Local v1, v2 = Val(FormatStr("%s%02s%02s", y1$, m1$, d1$)), Val(FormatStr("%s%02s%02s", y2$, m2$, d2$))
If v1 < v2 Then Return(-1)
If v1 > v2 Then Return(1)
Return(0)
EndFunction