Create a SORT command for gadget lists

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
marcoagpinto
Addict
Addict
Posts: 1039
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Create a SORT command for gadget lists

Post by marcoagpinto »

Hello!

Could you create such a command?

Right now I have to:
gadgetlist->copy to array->sort array->delete gadgetlist->copy to gadget list

This is very slow.

Code: Select all

  ; Sorts the list of synonyms
  ; V1.0 - 22/MAY/2013
  ;        o Just placed a warning window saying it is not coded yet.
  ; V1.1 - 24/MAY/2013
  ;        o Coded the complete procedure
  ; V1.2 - 27/MAY/2013
  ;        o Sort is now non case sensitive
  Procedure sort_synonyms()
    If number_of_synonyms=0
      MessageRequester("Warning","No synonyms found.")
      ProcedureReturn 0
    EndIf
      
    Dim synonyms_array$(number_of_synonyms-1)
    OpenGadgetList(#PANEL_MAIN,1)
    ProgressBarGadget(#NUMBER_SYNONYMS_PROGRESS_BAR,10+100+100+100+100+100-5-5,300-3+20+3,100,20-3, 0,number_of_synonyms*3)

    
    ; Copy from list to array
    AddWindowTimer(#WINDOW_MAIN,500,1)
    time=ElapsedMilliseconds()
    For f=0 To number_of_synonyms-1
      t$=GetGadgetItemText(#PANEL_BOX_SYNONYMS,f,0)
      For ff=1 To #max_number_meanings
        a$=GetGadgetItemText(#PANEL_BOX_SYNONYMS,f,ff)
        If a$<>"" : t$=t$+Chr(10)+a$ : EndIf
      Next ff
      synonyms_array$(f)=t$
      
      ; Fix to window stop responding if we click on it while adding synonyms
      event = WindowEvent()
      If Event = #PB_Event_Timer : EndIf      
      
      If ElapsedMilliseconds()>time+800
        SetGadgetState(#NUMBER_SYNONYMS_PROGRESS_BAR, f)
        time=ElapsedMilliseconds() 
      EndIf
    Next f    
    RemoveWindowTimer(#WINDOW_MAIN,500)
    
    
    ; Sort the array and delete the list 
    SortArray(synonyms_array$(),#PB_Sort_Ascending|#PB_Sort_NoCase)
    ClearGadgetItems(#PANEL_BOX_SYNONYMS)
    t=number_of_synonyms*2
    SetGadgetState(#NUMBER_SYNONYMS_PROGRESS_BAR,t) 


    ; copy from array back to list
    AddWindowTimer(#WINDOW_MAIN,500,1)
    time=ElapsedMilliseconds()
    For f=0 To number_of_synonyms-1
      t$=synonyms_array$(f)
      AddGadgetItem(#PANEL_BOX_SYNONYMS,-1,t$)
      
      ; Fix to window stop responding if we click on it while adding synonyms
      event = WindowEvent()
      If Event = #PB_Event_Timer : EndIf      
      
      If ElapsedMilliseconds()>time+800
        t=(number_of_synonyms*2)+f
        SetGadgetState(#NUMBER_SYNONYMS_PROGRESS_BAR, t)
        time=ElapsedMilliseconds() 
      EndIf
    Next f    
    RemoveWindowTimer(#WINDOW_MAIN,500)
    
    t=number_of_synonyms*3
    SetGadgetState(#NUMBER_SYNONYMS_PROGRESS_BAR,t)
    Delay(500)
    HideGadget(#NUMBER_SYNONYMS_PROGRESS_BAR,1)
    CloseGadgetList()
    
  EndProcedure
  
Thanks!

Kind regards,
>Marco A.G.Pinto
---------------
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Create a SORT command for gadget lists

Post by IdeasVacuum »

Having a sort command for lists would be very convenient, so +1 there, but it will not necessarily deliver a noticeable improvement in speed.

If you are coding for Windows only, then Gnozal's PureLvSort lib is for you: http://gnozal.ucoz.com/
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Create a SORT command for gadget lists

Post by MachineCode »

If you just want to add individual items to a ListView or ListIcon alphabetically, they have flags which'll do it for you (search).
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply