Sort ListIcon by column click

Share your advanced PureBasic knowledge/code with the community.
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Sort ListIcon by column click

Post by Max »

Hi,
inspired by NetMaestro code:
http://www.purebasic.fr/english/viewtopic.php?t=22209

Code: Select all

#LIdate  = "%dd-%mm-%yyyy"
#intDate = "%yyyy-%mm-%dd"
Global LIhwnd.l, LIgadID.l, LIdatlin.l, LIallcols.s, LItcol.s, LIcolold.l, LIupdown.l=1

macro LIcoltype(fld)
  fld.s = PeekS(LIDatlin)
  select LItcol
    case "N": fld = Rset(fld,15,"0")                              ; numeric col
    case "D": fld = FormatDate(#intDate,ParseDate(#LIdate, fld))  ; date    col
    default : fld = ucase(fld)                                    ; string  col
  endselect
endmacro

Procedure CompareColClick(item1, item2, lParamSort)
  result = 0
  lvi.LV_ITEM
  lvi\pszText    = LIdatlin
  lvi\cchTextMax = 512
  lvi\Mask       = #LVIF_TEXT
  lvi\iSubItem   = lParamSort
  SendMessage_(LIhwnd, #LVM_GETITEMTEXT, item1, @lvi)
  LIcoltype(first)
  SendMessage_(LIhwnd, #LVM_GETITEMTEXT, item2, @lvi)
  LIcoltype(second)
  if first > second: result =  1: endif
  if first < second: result = -1: endif
  if lParamSort = LIcolant: result = result * LIupdown: endif
  ProcedureReturn result
EndProcedure

Procedure ColumnClick(hwnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  Select uMsg
    Case #WM_NOTIFY
      *msg.NMHDR = lParam
      LIhwnd  = *msg\hwndFrom
      LIgadID = GetDlgCtrlID_(LIhwnd)
      if GadgetType(LIgadID) = #PB_GadgetType_ListIcon AND *msg\code = #LVN_COLUMNCLICK
        if MessageRequester("Sort", "Do you want to sort by this column?", #PB_MessageRequester_YesNoCancel) = #PB_MessageRequester_Yes
          SetCursor_(LoadCursor_(0, #IDC_WAIT))
          *pnmv.NM_LISTVIEW = lParam
          LIdatlin = AllocateMemory(512)
          LItcol = "A"
          ; here you can check for a lot of ListIcons in your window
          if val(StringField(LIallcols,1,":")) = LIgadID
            LItcol = mid(StringField(LIallcols,2,":"),*pnmv\iSubItem+1,1)
          endif
          SendMessage_(LIhwnd, #LVM_SORTITEMSEX, *pnmv\iSubItem, @CompareColClick())
          UpdateWindow_(LIhwnd)
          FreeMemory(LIdatlin)
          if LIcolant = *pnmv\iSubItem
                LIupdown = -LIupdown
          else: LIupdown = -1: endif
          LIcolant = *pnmv\iSubItem
          SetCursor_(LoadCursor_(0, #IDC_ARROW))
        endif
      endIf
  EndSelect
  ProcedureReturn result
EndProcedure

;-----------------------------------
; MAIN
;-----------------------------------
SetWindowCallback(@ColumnClick())

(some stuff... openwindow ... listicongadget...)

LIallcols = str(#Listicon)+":"+"AANAD"+":"   ; String of column types (see macro LIcoltype)


Repeat
   WaitWindowEvent
   (some stuff)
   ...
until #PB_Event_CloseWindow
END
User avatar
Caronte3D
Addict
Addict
Posts: 1056
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Sort ListIcon by column click

Post by Caronte3D »

Hi I'm using this code to sort listicon columns of numeric values, but when it have negative and positive ones, the sort doesn't work correctly.

*Example of bad descending:
10.50
6.10
4.60
-4.85 <-- wrong
-2.22 <-- wrong
-1.10 <-- wrong

*It should be:
10.50
6.10
4.60
-1.10 <-- ok
-2.22 <-- ok
-4.85 <-- ok

Anyone can give me a clue on how to modify the above code to do correct numeric sort?
Thanks in advance :wink:
User avatar
Caronte3D
Addict
Addict
Posts: 1056
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Sort ListIcon by column click

Post by Caronte3D »

* Post DELETED because doen't work
Last edited by Caronte3D on Fri May 08, 2020 7:21 am, edited 1 time in total.
User avatar
Caronte3D
Addict
Addict
Posts: 1056
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Sort ListIcon by column click

Post by Caronte3D »

The solution of the above post doesn't work correctly either :cry:
Anyone have a better solution?
User avatar
Caronte3D
Addict
Addict
Posts: 1056
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Sort ListIcon by column click

Post by Caronte3D »

Well.. I found a solution for the problem with negative & positive floats:

Code: Select all

  If Right(first,1)>="0" And Right(first,1)<="9"
    If ValD(first) > ValD(second): result =  1: EndIf
    If ValD(first) < ValD(second): result = -1: EndIf
  Else  
    If first > second: result =  1: EndIf
    If first < second: result = -1: EndIf    
  EndIf
It's only a solution for my exact code, so... you will need to adjust it for your own needs
Post Reply