PureLVSORT library : sorting ListIconGadgets (and more)

All PureFORM, JaPBe, Libs and useful code maintained by gnozal

Moderator: gnozal

QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by QuimV »

:D Thanks a lot Gnozal!
QuimV
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

thank you very much for this gnozal. you rock!
PureBasic 4.51(x86) on WinXP SP3
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

hello gnozal, i really find this lib extremely useful. however, i got stuck in a bit of a problem.
you see, one of my column is in currency format (i.e. $2,342.50, $549.00, etc) and currently purelvsort couldn't sort them using the existing templates
could you add another template pattern like "#PureLVSORT_Currency" which basically ignores all characters that are not "numeric" and "." and then sort them as floats?
PureBasic 4.51(x86) on WinXP SP3
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by Michael Vogel »

Not sure, if implementing such things would slow down sorting (maybe someone needs rational numbers etc.)...
...I would think about adding a hidden column containing the unretouched values -- this column could be sorted easily 8)
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

Michael Vogel wrote:Not sure, if implementing such things would slow down sorting (maybe someone needs rational numbers etc.)...
...I would think about adding a hidden column containing the unretouched values -- this column could be sorted easily 8)
i have considered this method but the problem is how to click the column header if the column i want to sort is hidden.
then i tried using PureLVSORT_DefineUserCallback but i encountered another roadblock because i couldn't make it work with predefined procedures.
id be grateful, if you could provide a simple working example how to do this.
PureBasic 4.51(x86) on WinXP SP3
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by gnozal »

liam wrote:then i tried using PureLVSORT_DefineUserCallback but i encountered another roadblock because i couldn't make it work with predefined procedures.
id be grateful, if you could provide a simple working example how to do this.
The user callback is quite easy to use.
Here is a basic example : column 0 uses a custom callback.

Code: Select all

;/ USER CALLBACK
Procedure.l MySortingCallback(ListIconNumber.l, ListIconColumn.l, Item1.s, Item2.s)
  Protected ReturnValue.l
  Debug Str(ListIconNumber) + ":" + Str(ListIconColumn) + "->" + Item1 + ":" + Item2
  ;
  ; Compare Item1 And Item2 ..
  ; Return  1 If Item1 > Item2
  ; Return -1 If Item1 < Item2
  ; Return  0 If Item1 = Item2
  ;
  If Item1 > Item2
    ReturnValue = 1
  ElseIf Item1 < Item2
    ReturnValue = -1
  Else
    ReturnValue = 0
  EndIf
  ProcedureReturn ReturnValue
EndProcedure
;/ Example
#Window_0 = 0
#ListIcon_0 = 0
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 216, 0, 602, 302, "PureLVSORT User Callback",   #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
    ListIconGadget(#ListIcon_0, 5, 5, 590, 285, "UserCallback", 110)
    AddGadgetColumn(#ListIcon_0, 1, "DateDDMMYYYYHHMMSS", 130)
    AddGadgetColumn(#ListIcon_0, 2, "DateDDMMYYHHMM", 130)
    AddGadgetColumn(#ListIcon_0, 3, "DateDDMMYYYY", 120)
    AddGadgetColumn(#ListIcon_0, 4, "DateMMDDYYYY", 120)
    AddGadgetColumn(#ListIcon_0, 5, "FileSize", 120)
    AddGadgetColumn(#ListIcon_0, 6, "UserCallback", 120)
    AddGadgetItem(#ListIcon_0, -1, "aseza" + Chr(10) + "12/05/2001 06:41:30" + Chr(10) + "19/07/66 06:41" + Chr(10) + "31/12/2004" + Chr(10) + "12/31/2004" + Chr(10) + "15.02 MB" + Chr(10) + "0")
    AddGadgetItem(#ListIcon_0, -1, "zssdd" + Chr(10) + "05/07/2004 09:21:30" + Chr(10) + "12/05/01 07:50" + Chr(10) + "11/12/2004" + Chr(10) + "12/11/2004"   + Chr(10) + "65 B" + Chr(10) + "1")
    AddGadgetItem(#ListIcon_0, -1, "aeeed" + Chr(10) + "19/11/2003 07:18:31" + Chr(10) + "13/08/03 06:41" + Chr(10) + "21/01/2003" + Chr(10) + "01/21/2003" + Chr(10) + "5.98 GB" + Chr(10) + "3")
    AddGadgetItem(#ListIcon_0, -1, "udsdd" + Chr(10) + "19/11/2003 06:21:30" + Chr(10) + "12/05/01 06:41" + Chr(10) + "10/06/2001" + Chr(10) + "06/10/2001" + Chr(10) + "100 KB" + Chr(10) + "A")
    AddGadgetItem(#ListIcon_0, -1, "cdgdd" + Chr(10) + "19/11/2003 16:21:30" + Chr(10) + "12/05/01 06:41" + Chr(10) + "10/08/2001" + Chr(10) + "16/11/2001" + Chr(10) + "800 KB" + Chr(10) + "9")
    AddGadgetItem(#ListIcon_0, -1, "adsdg" + Chr(10) + "19/11/2003 06:21:31" + Chr(10) + "12/05/01 06:41" + Chr(10) + "10/06/2004" + Chr(10) + "06/10/2004" + Chr(10) + "101 KB" + Chr(10) + "z")
    AddGadgetItem(#ListIcon_0, -1, "azsdd" + Chr(10) + "19/11/2003 06:21:30" + Chr(10) + "12/05/01 06:41" + Chr(10) + "10/04/2001" + Chr(10) + "07/08/2004" + Chr(10) + "1000 B" + Chr(10) + "A")
  EndIf
EndProcedure
Open_Window_0()
; ListIcon Sort Setup
If PureLVSORT_SelectGadgetToSort(#ListIcon_0, #PureLVSORT_ShowClickedHeader_IconLeft) = #PureLVSORT_Ok
  PureLVSORT_DefineUserCallback(@MySortingCallback())
  PureLVSORT_SetColumnType(#ListIcon_0, 0, #PureLVSORT_UserCallback)
  PureLVSORT_SetColumnType(#ListIcon_0, 1, #PureLVSORT_DateDDMMYYYYHHMMSS)
  PureLVSORT_SetColumnType(#ListIcon_0, 2, #PureLVSORT_DateDDMMYYHHMM)
  PureLVSORT_SetColumnType(#ListIcon_0, 3, #PureLVSORT_DateDDMMYYYY)
  PureLVSORT_SetColumnType(#ListIcon_0, 4, #PureLVSORT_DateMMDDYYYY)
  PureLVSORT_SetColumnType(#ListIcon_0, 5, #PureLVSORT_FileSize)
  PureLVSORT_SetColumnType(#ListIcon_0, 6, #PureLVSORT_UserCallback)
  PureLVSORT_SortListIconNow(#ListIcon_0, 1, -1)
EndIf
;
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
End
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

thank you for the prompt reply gnozal but im still confused how to use call backs to sort the list according to another (hidden) column.

lets say i have this listicon data below. how do i sort the rows according to the 2nd column by clicking the 1st column header? im still learning PB programming so please bear with me :|

Code: Select all

;/ USER CALLBACK
Procedure.l MySortingCallback(ListIconNumber.l, ListIconColumn.l, Item1.s, Item2.s)
  Protected ReturnValue.l
  Debug Str(ListIconNumber) + ":" + Str(ListIconColumn) + "->" + Item1 + ":" + Item2
  ;
  ; Compare Item1 And Item2 ..
  ; Return  1 If Item1 > Item2
  ; Return -1 If Item1 < Item2
  ; Return  0 If Item1 = Item2
  ;
  If Item1 > Item2
    ReturnValue = 1
  ElseIf Item1 < Item2
    ReturnValue = -1
  Else
    ReturnValue = 0
  EndIf
  ProcedureReturn ReturnValue
EndProcedure
;/ Example
#Window_0 = 0
#ListIcon_0 = 0
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 216, 0, 602, 302, "PureLVSORT User Callback",   #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
    ListIconGadget(#ListIcon_0, 5, 5, 590, 285, "UserCallback", 110)
    AddGadgetColumn(#ListIcon_0, 1, "UserCallback", 120)
    AddGadgetItem(#ListIcon_0, -1, "$36.00" + Chr(10) + "36.00")
    AddGadgetItem(#ListIcon_0, -1, "$324.00" + Chr(10) + "324.00")
    AddGadgetItem(#ListIcon_0, -1, "$324,432.45" + Chr(10) + "324,432.45")
    AddGadgetItem(#ListIcon_0, -1, "$2,561.45" + Chr(10) + "2561.45")
  EndIf
EndProcedure
Open_Window_0()
; ListIcon Sort Setup
If PureLVSORT_SelectGadgetToSort(#ListIcon_0, #PureLVSORT_ShowClickedHeader_IconLeft) = #PureLVSORT_Ok
  PureLVSORT_DefineUserCallback(@MySortingCallback())
  PureLVSORT_SetColumnType(#ListIcon_0, 0, #PureLVSORT_UserCallback)
  PureLVSORT_SetColumnType(#ListIcon_0, 1, #PureLVSORT_NoSorting)
EndIf
;
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
End
PureBasic 4.51(x86) on WinXP SP3
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by gnozal »

liam wrote:lets say i have this listicon data below. how do i sort the rows according to the 2nd column by clicking the 1st column header? im still learning PB programming so please bear with me :|
With PureLVSORT, the sorting is done according to the column you clicked on.
I don't understand why you need a hidden column.
See the example below. When you click on the 1st column, the data is sorted correctly, because the data is converted in the user callback.

Code: Select all

;/ USER CALLBACK
Procedure.l MySortingCallback(ListIconNumber.l, ListIconColumn.l, Item1.s, Item2.s)
  Protected ReturnValue.l
  Protected Item1D.d, Item2D.d
  ;
  ; Quick and dirty transforming Currency to DOUBLE
  Item1 = RemoveString(Item1, "$")
  Item1 = RemoveString(Item1, ",")
  Item1D = ValD(Item1)
  Item2 = RemoveString(Item2, "$")
  Item2 = RemoveString(Item2, ",")
  Item2D = ValD(Item2)
  ;
  If Item1D > Item2D
    ReturnValue = 1
  ElseIf Item1D < Item2D
    ReturnValue = -1
  Else
    ReturnValue = 0
  EndIf
  ProcedureReturn ReturnValue
EndProcedure
;/ Example
#Window_0 = 0
#ListIcon_0 = 0
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 216, 0, 602, 302, "PureLVSORT User Callback",   #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
    ListIconGadget(#ListIcon_0, 5, 5, 590, 285, "UserCallback", 110)
    AddGadgetColumn(#ListIcon_0, 1, "No sorting", 120)
    AddGadgetItem(#ListIcon_0, -1, "$36.00" + Chr(10) + "36.00")
    AddGadgetItem(#ListIcon_0, -1, "$324.00" + Chr(10) + "324.00")
    AddGadgetItem(#ListIcon_0, -1, "$324,432.45" + Chr(10) + "324,432.45")
    AddGadgetItem(#ListIcon_0, -1, "$2,561.45" + Chr(10) + "2561.45")
  EndIf
EndProcedure
Open_Window_0()
; ListIcon Sort Setup
If PureLVSORT_SelectGadgetToSort(#ListIcon_0, #PureLVSORT_ShowClickedHeader_IconLeft) = #PureLVSORT_Ok
  PureLVSORT_DefineUserCallback(@MySortingCallback())
  PureLVSORT_SetColumnType(#ListIcon_0, 0, #PureLVSORT_UserCallback)
  PureLVSORT_SetColumnType(#ListIcon_0, 1, #PureLVSORT_NoSorting)
EndIf
;
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
End
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

gnozal wrote:
liam wrote:lets say i have this listicon data below. how do i sort the rows according to the 2nd column by clicking the 1st column header? im still learning PB programming so please bear with me :|
With PureLVSORT, the sorting is done according to the column you clicked on.
I don't understand why you need a hidden column.

if you'll backtrack a few posts, my initial problem is about sorting a column with numeric values in 'currency' formatting i.e. "$36.00", "$324.00", "$324,432.45", "$2,561.45" and so on. i couldnt use #PureLVSORT_Numeric, #PureLVSORT_Numeric_Quad, #PureLVSORT_Float on that column because it doesn't work due to the extra characters "$" and "," hence i requested that you also include a sorting template like #PureLVSORT_Currency which ignores all non-numeric characters and sorts it like a float.
michael vogel suggested that i use a 'hidden' column containing the unformatted values which could be sorted easily, this is where the issue of using usercallbacks came in. im not sure though how to implement usercallbacks to address my problem.
so if you could implement #PureLVSORT_Currency format, it could save a lot of people like me a lot of hair :D
PureBasic 4.51(x86) on WinXP SP3
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by gnozal »

liam wrote:...im not sure though how to implement usercallbacks to address my problem.
so if you could implement #PureLVSORT_Currency format, it could save a lot of people like me a lot of hair :D
Did you try the code I posted ? It shows how to sort a column by transforming a format ('currency') to another (double float).

I did not implement a currency format because it does not exist in Purebasic. This is a case where one should use the user callback feature.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

Did you try the code I posted ? It shows how to sort a column by transforming a format ('currency') to another (double float).
i tried the code you posted, actually its identical to the sample code in PureLVSORT lib's documentation. all i see are strings, dates, filesizes but no currency-to-float conversions. i even posted a reply with your sample code and my sample currency data but it didn't work.
PureBasic 4.51(x86) on WinXP SP3
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by gnozal »

liam wrote:i tried the code you posted, actually its identical to the sample code in PureLVSORT lib's documentation. all i see are strings, dates, filesizes but no currency-to-float conversions. i even posted a reply with your sample code and my sample currency data but it didn't work.
I mean this code I posted not one hour ago :

Code: Select all

;/ USER CALLBACK
Procedure.l MySortingCallback(ListIconNumber.l, ListIconColumn.l, Item1.s, Item2.s)
  Protected ReturnValue.l
  Protected Item1D.d, Item2D.d
  ;
  ; Quick and dirty transforming Currency to DOUBLE
  Item1 = RemoveString(Item1, "$")
  Item1 = RemoveString(Item1, ",")
  Item1D = ValD(Item1)
  Item2 = RemoveString(Item2, "$")
  Item2 = RemoveString(Item2, ",")
  Item2D = ValD(Item2)
  ;
  If Item1D > Item2D
    ReturnValue = 1
  ElseIf Item1D < Item2D
    ReturnValue = -1
  Else
    ReturnValue = 0
  EndIf
  ProcedureReturn ReturnValue
EndProcedure
;/ Example
#Window_0 = 0
#ListIcon_0 = 0
Procedure Open_Window_0()
  If OpenWindow(#Window_0, 216, 0, 602, 302, "PureLVSORT User Callback",   #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
    ListIconGadget(#ListIcon_0, 5, 5, 590, 285, "UserCallback", 110)
    AddGadgetColumn(#ListIcon_0, 1, "No sorting", 120)
    AddGadgetItem(#ListIcon_0, -1, "$36.00" + Chr(10) + "36.00")
    AddGadgetItem(#ListIcon_0, -1, "$324.00" + Chr(10) + "324.00")
    AddGadgetItem(#ListIcon_0, -1, "$324,432.45" + Chr(10) + "324,432.45")
    AddGadgetItem(#ListIcon_0, -1, "$2,561.45" + Chr(10) + "2561.45")
  EndIf
EndProcedure
Open_Window_0()
; ListIcon Sort Setup
If PureLVSORT_SelectGadgetToSort(#ListIcon_0, #PureLVSORT_ShowClickedHeader_IconLeft) = #PureLVSORT_Ok
  PureLVSORT_DefineUserCallback(@MySortingCallback())
  PureLVSORT_SetColumnType(#ListIcon_0, 0, #PureLVSORT_UserCallback)
  PureLVSORT_SetColumnType(#ListIcon_0, 1, #PureLVSORT_NoSorting)
EndIf
;
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
End
It's obviously not identical to the sample code in PureLVSORT lib's documentation.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
liam
User
User
Posts: 38
Joined: Tue Jul 03, 2007 3:48 am
Location: Philippines

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by liam »

ok now i see it! damn.. i think i need some sleep, i wont even recognize a cobra in front of my face.
i just tried it and its working! thanks. im gonna dissect and digest this one after some rest. thank you once again!
PureBasic 4.51(x86) on WinXP SP3
rule
User
User
Posts: 14
Joined: Sat Sep 02, 2006 7:56 pm
Location: Oostzaan / Netherlands

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by rule »

Hi Gnozal,
I've tried to run one of your example programs PureLVSORT_TEST_2.pb and I get a Polink error "Unresolved external symbol '_PB_Findstring2.'
Tried it on XPsp3 and Windows 7. It seems to happen on the line "PureLVSORT_SelectGadgetToSort()". I dont get this error if I comment this line out.

Thanks,
Roel.

Oh, I use the latest jaPBe, PURELVSORT and PureBasic 4.51
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: PureLVSORT library : sorting ListIconGadgets (and more)

Post by gnozal »

rule wrote:I've tried to run one of your example programs PureLVSORT_TEST_2.pb and I get a Polink error "Unresolved external symbol '_PB_Findstring2.'
It looks like your are using the wrong library version.
With PB4.51, you should be using PureLVSORT_450.zip.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply