It is currently Sun Jan 19, 2020 5:34 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: New flag fort sort functions: #PB_Sort_EmptyLast
PostPosted: Sat Jan 11, 2020 9:50 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jun 11, 2004 7:07 am
Posts: 609
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", ""


Top
 Profile  
Reply with quote  
 Post subject: Re: New flag fort sort functions: #PB_Sort_EmptyLast
PostPosted: Sat Jan 11, 2020 12:18 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 496
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: New flag fort sort functions: #PB_Sort_EmptyLast
PostPosted: Sat Jan 11, 2020 1:22 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 959
@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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: New flag fort sort functions: #PB_Sort_EmptyLast
PostPosted: Sat Jan 11, 2020 1:38 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Apr 18, 2019 8:17 am
Posts: 496
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:
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:
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. :)


Top
 Profile  
Reply with quote  
 Post subject: Re: New flag fort sort functions: #PB_Sort_EmptyLast
PostPosted: Sat Jan 11, 2020 3:15 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3729
Location: Berlin, Germany
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.

_________________
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  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: nsstudios and 4 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