Sorting by Date in listview - best strategy?

Discuss GUI programming with the RapaGUI plugin here
Post Reply
gerograph
Posts: 56
Joined: Thu Mar 04, 2010 9:38 pm

Sorting by Date in listview - best strategy?

Post by gerograph »

Hi, it is me again :-)....next question concerning my ToDo List projekt ("ticklish"):

I made 2 columns "sortable" via my xml file:

Code: Select all

<listview height="200" id="lstv_main" notify="active; LongClick; ClickColumn; SortColumn">
	<column align="center" width="50" title="Status" checkbox="true"></column>
	<column title="Description" width="100" editable="true" sortable="true"></column>
	<column title="Date" editable="true" sortable="true"></column>
When clicking on the column titel "Description" all items will be sorted alphabetically = o.k.
When clicking on the column titel "Date" all items will be sorted alphabetically = but I want them sorted by Date...

As far as I have read, listviews "inbuild sortfunction" is always alphabetically, and there is no chance to switch to another sort order, correct?
Ideas for an easy strategy to overcome this problem would be very wellcome:

Idea 1:
Force the user to enter Date in YYYY-MM-DD...would make it sortable, hower displaying Dates in such a format is not really an option...

idea 2:
???
plouf
Posts: 666
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Sorting by Date in listview - best strategy?

Post by plouf »

You can create a custom sort using Listview.CompareItems()

https://www.hollywood-mal.com/docs/html ... tems_.html

You can also store date using YYYY-MM-DD format which alys is arithemtic correct in sort :-)
Christos
gerograph
Posts: 56
Joined: Thu Mar 04, 2010 9:38 pm

Re: Sorting by Date in listview - best strategy?

Post by gerograph »

@plouf
I will give that one a try....but not sure yet, how that one will work.... I'll report back
gerograph
Posts: 56
Joined: Thu Mar 04, 2010 9:38 pm

Re: Sorting by Date in listview - best strategy?

Post by gerograph »

Okay thankyou...this (viewtopic.php?p=20135#p20135) was quiet helpful*. So here is my solution:

Code: Select all

@REQUIRE "RapaGUI"
@FILE 1, "sorttest.xml"

moai.CreateApp(FileToString("sorttest.xml"))

t = {"11.11.2025", "11.11.2027", "1.1.2028", "3.3.1972"}
f = {"Birkenfeld", "Meier", "Anton", "Zappel"}

Function p_CompareFunc(e1, e2)

/******* split up Datestring and rearrange to YYYYMMDD ****/
	If tv = 0 ;a date based column
		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)
	EndIf
	
	If tv = 1 ;plain alphabetical column
		If e1 < e2 Then Return(-1)
		If e1 > e2 Then Return(1)
	EndIf

	Return(0)

EndFunction

InstallEventHandler({RapaGUI = Function(msg)
	If msg.attribute = "SortColumn"
		tv = msg.triggervalue ;holds the column number you pressed...
	EndIf
	If msg.attribute = "CompareItems" Then Return(p_CompareFunc(msg.entry1, msg.entry2))
	If msg.attribute = "Pressed"
		If msg.ID = "btn_datum"
			DebugPrint("Datum sort")
		ElseIf msg.ID = "btn_name"
			DebugPrint("Name sort")
		EndIf
	EndIf
EndFunction})

For Local k = 0 To ListItems(t) - 1
	moai.DoMethod("lv", "insert", "bottom", t[k], f[k]) 
Next

Repeat
	WaitEvent
Forever 
This one is the important bit (for me) as it calls the procedure "p_compareFunc()", every time two items have to be compared (the longer the list,the more often this functions will be called, I guess):

Code: Select all

If msg.attribute = "CompareItems" Then Return(p_CompareFunc(msg.entry1, msg.entry2))
Inside the mentioned function I react differently, according wether the date or the name column was clicked. Here is the xml file as well:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app" useicons="true">
	<window title="Messages und Listen">
		<vgroup>
			<hgroup>
				<button id="btn_datum">Datum</button>
				<button id="btn_name">Name</button>
			</hgroup>
			<listview id="lv" notify="active; CompareItems;SortColumn">
				<column title="Spalte0" sortable="true" editable="true"></column>
				<column title="Spalte1" sortable="true" editable="true"></column>
			</listview>
		</vgroup>
	</window>
</application>


*already read it yesterday...but had to dig a bit deeper to understand.
Post Reply