New flag fort sort functions: #PB_Sort_EmptyLast

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

New flag fort sort functions: #PB_Sort_EmptyLast

Post 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", ""
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Post by BarryG »

Incorrect example removed. See new post further below.
Last edited by BarryG on Sat Jan 11, 2020 1:35 pm, edited 1 time in total.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Post 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.
sorry for my bad english
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Post 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. :)
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: New flag fort sort functions: #PB_Sort_EmptyLast

Post 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.
Post Reply