Problem to sort numbers
Problem to sort numbers
Hello,
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!
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!
Re: Problem to sort numbers
its obvius that they are sorted by as string, see the first 3 characters that this sort is ok, afaik they are always strings
imho you should , extract data, sort them manualy and throu back in order.
you have discuss this a few times in past, but you dont provide some working example so we can understand what you practically do
if, for example, you extract data out of a database (SQLite ?), you can ask database to export them in order for you
imho you should , extract data, sort them manualy and throu back in order.
you have discuss this a few times in past, but you dont provide some working example so we can understand what you practically do
if, for example, you extract data out of a database (SQLite ?), you can ask database to export them in order for you
Christos
- airsoftsoftwair
- Posts: 5461
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Problem to sort numbers
You could also specify a custom sort callback using Listview.CompareItems to solve the problem.
Re: Problem to sort numbers
does this change listview logic from "text" to "number" ? and if yes what else can understand ? date ? currency?
Christos
Re: Problem to sort numbers
@plouf: it's a JSON import, the sort is an option for many columns (date, name and numbers) in this project.
Personnaly, i understand why the sort don't work correctly, i just want a sort increase or decrease by number.
The sort should be the same for all type of datas:
A->Z then second letter, etc.
01-01-1900 -> 01-01-2024
0.0001 -> 999.9999
No?
I understand why i must do a custom sort callback using Listview.CompareItems to do a simple sort by number.
@Andreas: do you have an example of custom sort callback please?
Could you correct this problem for next RAPAGui please?
Personnaly, i understand why the sort don't work correctly, i just want a sort increase or decrease by number.
The sort should be the same for all type of datas:
A->Z then second letter, etc.
01-01-1900 -> 01-01-2024
0.0001 -> 999.9999
No?
I understand why i must do a custom sort callback using Listview.CompareItems to do a simple sort by number.
@Andreas: do you have an example of custom sort callback please?
Could you correct this problem for next RAPAGui please?
- airsoftsoftwair
- Posts: 5461
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Problem to sort numbers
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
Re: Problem to sort numbers
Thanks a lot Andreas for your example!
Maybe should be nice to add an option in listview declaration like type="numeric", "date", "alphabetic" to have a specific sort without a custum sort.
I have watched your example but how i must do if i have 1500 entries? My problem here, i add new entries regulary...
Maybe should be nice to add an option in listview declaration like type="numeric", "date", "alphabetic" to have a specific sort without a custum sort.
I have watched your example but how i must do if i have 1500 entries? My problem here, i add new entries regulary...
Re: Problem to sort numbers
soory, i would like to say "column" declaration and not listview...
Re: Problem to sort numbers
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.
i try with your example the following
Code: Select all
t = {"21/12/2020","4/5/2021","30/01/1651","12/12/1900"}
Christos
Re: Problem to sort numbers
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"}
The previous example was only to convert numerical strings to numbers and compare them. Now when you have string data that has slashes in it, Val("21/12/2020") just gives 21 as the result. So you're comparing the data by the day numbers, not with the whole date.
Here's an example logic to compare dates in the format you gave (haven't thought if it's optimal by speed, but an example anyway):
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
As told, you can write any kind sorting algorithms in this way, everything is possible But of course you have to modify the sorting algorithm to match your data.
I myself have written custom sorting algorithms in RNOArchive and RNOTunes, and probably somewhere else too. I have usually used Hollywood's own date format for dates, and then the compare function can just use CompareDates() function to determine the order. I also have made some generic sorting functions that operate in different ways according the column that has been clicked and have reversed sorting if clicking the same column again.