Page 1 of 1

New flag fort sort functions: #PB_Sort_EmptyLast

Posted: Sat Jan 11, 2020 9:50 am
by Lebostein
At the moment empty strings at the beginning: "", "1", "A", "B"
Would nice to have on option to sort empty strings at the bottom of the list: "1", "A", "B", ""

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Posted: Sat Jan 11, 2020 12:18 pm
by BarryG
Incorrect example removed. See new post further below.

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Posted: Sat Jan 11, 2020 1:22 pm
by Josh
@BarryG

This is only in your example, because the blanks are already at positions 9+10 and you only sort the first 8 positions. Here isn't so:

Code: Select all

ten=10
Dim a$(ten)

a$(1)  = "A"
a$(2)  = "D"
a$(3)  = "H"
a$(4)  = "R"
a$(5)  = ""
a$(6)  = "S"
a$(7)  = "S"
a$(8)  = "U"
a$(9)  = ""
a$(10) = "V"

SortArray(a$(),#PB_Sort_Ascending,1,8) ; ",1,8" forces blanks to the bottom.

For a=1 To ten
  Debug ">"+a$(a)+"<"
Next

However, I think it is not a big effort to move the empty strings 'manually' to the end.

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Posted: Sat Jan 11, 2020 1:38 pm
by BarryG
Right. I totally overlooked that. :oops:

Here's two (hacky and probably slow) solutions, so I agree #PB_Sort_EmptyLast is a good wish!

This one does the request and keeps the same number of elements:

Code: Select all

count=10
Dim a$(count)

a$(1)  = "Z"
a$(2)  = "D"
a$(3)  = "B"
a$(4)  = "R"
a$(5)  = ""
a$(6)  = "S"
a$(7)  = "A"
a$(8)  = "U"
a$(9)  = ""
a$(10) = "E"

SortArray(a$(),#PB_Sort_Descending)
For n=1 To count
  If a$(n)=""
    ReDim a$(n)
    SortArray(a$(),#PB_Sort_Ascending)
    ReDim a$(count)
    Break
  EndIf
Next

For a=1 To count
  Debug ">"+a$(a)+"<"
Next
And this one does the request but reduces the number of elements by removing the empty elements:

Code: Select all

count=10
Dim a$(count)

a$(1)  = "Z"
a$(2)  = "D"
a$(3)  = "B"
a$(4)  = "R"
a$(5)  = ""
a$(6)  = "S"
a$(7)  = "A"
a$(8)  = "U"
a$(9)  = ""
a$(10) = "E"

SortArray(a$(),#PB_Sort_Descending)
For n=1 To count
  If a$(n)=""
    ReDim a$(n)
    SortArray(a$(),#PB_Sort_Ascending)
    count=n
    Break
  EndIf
Next

For a=1 To count
  Debug ">"+a$(a)+"<"
Next
Take your pick, Lebostein. :)

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Posted: Sat Jan 11, 2020 3:15 pm
by Little John
Lebostein wrote:At the moment empty strings at the beginning: "", "1", "A", "B"
... if and only if the option #PB_Sort_Ascending is used.
Lebostein wrote:New flag fort sort functions: #PB_Sort_EmptyLast
Naming the additionally wanted flag #PB_Sort_EmptyLast wouldn't be a good idea, because PB puts empty strings at the end anyway when the option #PB_Sort_Descending is used.
A more logical name would be e.g. #PB_Sort_EmptyOpposite:
- put empty strings at the end when sorting ascending
- put empty strings at the beginning when sorting descending

I posted a procedure SortStringArraySpecial() that does the job.