# PureBasic Forum

 It is currently Sat Feb 22, 2020 7:31 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 1 post ]
 Print view Previous topic | Next topic
Author Message
 Post subject: SortStringArraySpecial()Posted: Sat Jan 11, 2020 3:06 pm
 Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3759
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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 1 post ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 6 guests

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

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite

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