Mbee

Question: _ArraySort() output - Shortest string always first?

7 posts in this topic

I'm sorting an array of strings, alphabetically ascending. It's essential for my algorithm that the shortest of the variant strings always appear first.  That is, if the original array contains the following:

potential
potentiometer
potent
Potentials

... the output array must always have the following order:

potent
potential
Potentials
potentiometer

It seems logical that this would always be the case, but I need to be certain.  Will this always be the case?

 

Thanks!

Share this post


Link to post
Share on other sites



The short answer is yes for strings. For numbers no because the size is what counts.

1 person likes this

Share this post


Link to post
Share on other sites

Thanks, czardas!  I appreciate the confirmation.

1 person likes this

Share this post


Link to post
Share on other sites

for strings its not what counts either, unless you already know which sets go together and they arent already sorted alphabetically...

hug
huge
hugs
hugest
huggers

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Unless I misunderstood, the point is that the strings will all begin with the same sequence. Then longer strings are always greater than smaller strings within these sets. You can get a greater understanding by trying StringCompare(), which is the comparison function used for sorting strings.

hug
huge
hugest
huggers
hugs

Huggers has a new  4th character 'g' which is greater than 'e', so alphabetical sequences always take precedence (naturally!). Then anything that comes after the duplicated sequence is considered to be greater: just like in a dictionary.

If you are using this to replace strings; then loop through, or sort, the array in reverse. This will avoid replacing half a word by accident. It's also quite easy to create a string length sort function.

Edited by czardas

Share this post


Link to post
Share on other sites

Yeah, after thinking more the groups would have to be preset or you'd be boned.

I was more thinking that huge and hugest should be in their own list according to OP rules, but there would be no feasible way to discern that sort of thing.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

It's how I interpreted the question. In the list, the word potentiometer breaks the pattern at the 8th character (...ometer). With the other three; once sorted, shorter words are completely contained within the longer versions (the start of the pattern repeats in its entirety - each time growing in size).

The question may seem basic, but it's not necessarily obvious. I did some similar tests myself recently because I wasn't sure if the strings '1.' and '1.0' would be considered equal: as strings they are not. The character '0' is greater than no character at all! :blink: I guess that's good thinking.

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now