It is currently Sun Jan 26, 2020 3:05 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: SortStringArraySpecial()
PostPosted: Sat Jan 11, 2020 3:06 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3733
Location: Berlin, Germany
Special handling of empty strings when sorting a string array. For details see comments in the code.
There was a request for this.

Code:
; tested with PB 5.71 LTS

EnableExplicit


Procedure SortStringArraySpecial (Array a$(1), options.i, first.i=0, last.i=-1)
   ; After a (range of a) string array has been sorted ascending with PB's built-in function,
   ; empty strings are at the *beginning* of the array (or of the chosen range).
   ; When sorting descending, empty strings are put at the *end*.
   ;
   ; This procedure does almost the same as PB's built-in function SortArray(), but
   ; - puts empty strings at the end when sorting ascending
   ; - puts empty strings at the beginning when sorting descending
   Protected.i i, offset
   
   If last = -1
      last = ArraySize(a$())
   EndIf   
   
   SortArray(a$(), options, first, last)
   
   If options & #PB_Sort_Descending
      ; Empty strings are at the end of the sorted range.
      ; Now put them at the beginning of that range.
     
      ; Count number of empty strings at the end.
      For i = last To first Step -1
         If Asc(a$(i)) <> ''
            Break
         EndIf   
      Next   
      offset = last - i
     
      If offset > 0 And offset < last-first+1
         ; Move non-empty elements towards the end
         For i = last To first+offset Step -1
            a$(i) = a$(i-offset)
         Next
         
         ; Set elements at the beginning of the range to ""
         For i = first+offset-1 To first Step -1
            a$(i) = ""
         Next
      EndIf
     
   Else   
      ; Empty strings are at the beginning of the sorted range.
      ; Now put them at the end of that range.
     
      ; Count number of empty strings at the beginning.
      For i = first To last
         If Asc(a$(i)) <> ''
            Break
         EndIf   
      Next   
      offset = i - first
     
      If offset > 0 And offset < last-first+1
         ; Move non-empty elements towards the beginning
         For i = first To last-offset
            a$(i) = a$(i+offset)
         Next
         
         ; Set elements at the end of the range to ""
         For i = last-offset+1 To last
            a$(i) = ""
         Next   
      EndIf   
   EndIf   
EndProcedure


; -- Demo
Define.i i, lastElement = 6
Dim a$(lastElement)
Dim b$(lastElement)

; Create original array
a$(1) = "b"
a$(3) = "a"
a$(5) = "d"
a$(6) = "c"

Debug "Sorted ascending with PB's built-in function:"
CopyArray(a$(), b$())
SortArray(b$(), #PB_Sort_Ascending)
For i = 0 To lastElement
   Debug "'" + b$(i) + "'"
Next   

Debug ""
Debug "Sorted ascending with SortStringArraySpecial():"
CopyArray(a$(), b$())
SortStringArraySpecial(b$(), #PB_Sort_Ascending)
For i = 0 To lastElement
   Debug "'" + b$(i) + "'"
Next   

Debug ""
Debug "Sorted descending with PB's built-in function:"
CopyArray(a$(), b$())
SortArray(b$(), #PB_Sort_Descending)
For i = 0 To lastElement
   Debug "'" + b$(i) + "'"
Next   

Debug ""
Debug "Sorted descending with SortStringArraySpecial():"
CopyArray(a$(), b$())
SortStringArraySpecial(b$(), #PB_Sort_Descending)
For i = 0 To lastElement
   Debug "'" + b$(i) + "'"
Next

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: mk-soft and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye