Jump to content

order the Array from big string to small


Recommended Posts

Hello i  have a array that have lot of strings text & it is random . i need to make it from long text to the short

like

--------------------------

yesyesyesyesyes

yesyesyes

yesyes

yes

 

the source is

 

#include <Array.au3>

$ss=StringSplit("yesyes|yesyesyesyes|yesyesyes|yes|yesyesyesyesyeseys",'|')

_ArrayDisplay($ss, "reasult randome")

 

 

Edited by rayane888
Link to comment
Share on other sites

Hello @rayane888

There you go:

#include <Array.au3>

$ss=StringSplit("yesyes|yesyesyesyes|yesyesyes|yes|yesyesyesyesyeseys",'|')

_ArrayDisplay($ss, "reasult randome")

_Arraysort($ss, 1)

_ArrayDisplay($ss, "sorted result")

Enjoy :)

Edited by Neutro
Link to comment
Share on other sites

51 minutes ago, Neutro said:

Hello @rayane888

There you go:

#include <Array.au3>

$ss=StringSplit("yesyes|yesyesyesyes|yesyesyes|yes|yesyesyesyesyeseys",'|')

_ArrayDisplay($ss, "reasult randome")

_Arraysort($ss, 1)

_ArrayDisplay($ss, "sorted result")

Enjoy :)

thx but if we have diferent string not same how i do ???

like

yes123yes

yesvvvsdsdsdsyes

yes9999yesyes

 

 

like this exemple

 

#include <Array.au3>

$ss=StringSplit("yec21ds|yesyyesqyedsdd|yesye987ddyes|yexxss|yesyesg9999yesyesyeseydddddds",'|')

_ArrayDisplay($ss, "reasult randome")

_Arraysort($ss, 1)

_ArrayDisplay($ss, "sorted result")

 

 

Edited by rayane888
Link to comment
Share on other sites

This is one way of doing it:

#include <Array.au3>

$ss=StringSplit("yec21ds|yesyyesqyedsdd|yesye987ddyes|yexxss|yesyesg9999yesyesyeseydddddds",'|')

_ArrayDisplay($ss, "reasult randome")

_ArrayColInsert($ss, 1)

_ArrayDisplay($ss, "column inserted")

;put the string length of each string in the 2nd column

for $i = 1 to $ss[0][0]

    $ss[$i][1] = StringLen($ss[$i][0])

Next

_ArrayDisplay($ss, "length of all strings added")

_ArraySort($ss, 1, 1, 0, 1) ;sort the second column

_ArrayDisplay($ss, "sorted result by string length")

_ArrayColDelete($ss, 1) ; remove second column

_ArrayDisplay($ss, "final result")

Basicly you add a column to your array, store the length of each string in this new column then use this column to sort the array then delete the column!

Link to comment
Share on other sites

14 minutes ago, Neutro said:

This is one way of doing it:

#include <Array.au3>

$ss=StringSplit("yec21ds|yesyyesqyedsdd|yesye987ddyes|yexxss|yesyesg9999yesyesyeseydddddds",'|')

_ArrayDisplay($ss, "reasult randome")

_ArrayColInsert($ss, 1)

_ArrayDisplay($ss, "column inserted")

;put the string length of each string in the 2nd column

for $i = 1 to $ss[0][0]

    $ss[$i][1] = StringLen($ss[$i][0])

Next

_ArrayDisplay($ss, "length of all strings added")

_ArraySort($ss, 1, 1, 0, 1) ;sort the second column

_ArrayDisplay($ss, "sorted result by string length")

_ArrayColDelete($ss, 1) ; remove second column

_ArrayDisplay($ss, "final result")

Basicly you add a column to your array, store the length of each string in this new column then use this column to sort the array then delete the column!

man you are are the best  thx lot

Link to comment
Share on other sites

The alternative is to use a sorting function where you can use a user defined comparison function.
In the attachement is an udf for doing this. With this the result can look like this: 

#include <DynArray.au3>

$ss=StringSplit("yec21ds|yesyyesqyedsdd|yesye987ddyes|yexxss|yesyesg9999yesyesyeseydddddds",'|', 2)

_ArrayDisplay($ss, "reasult randome")
_ArraySortFlexible($ss, _MyCompare)
_ArrayDisplay($ss, "sorted result")

; user defined compare function for individual sorting
Func _MyCompare(ByRef $A, ByRef $B)
    Local $dA = StringLen($A), $dB = StringLen($B)
    Return $dA > $dB ? -1 : $dA < $dB ? 1 : 0
EndFunc

 

 

DynArray.au3

Edited by AspirinJunkie
Link to comment
Share on other sites

or just walk the array yourself?

#include <Array.au3>

$Ass=StringSplit("yec21ds|yesyyesqyedsdd|yesye987ddyes|yexxss|yesyesg9999yesyesyeseydddddds",'|' , 2)

For $i = ubound($Ass) - 1 to 1 step -1
    If StringLen($Ass[$i]) <  StringLen($Ass[$i - 1]) Then
        _ArraySwap($Ass , $i , $i - 1)
        $i = ubound($Ass)
    EndIf
Next

_ArrayDisplay($Ass)

 

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

Link to comment
Share on other sites

If you chose AutoIt for speed of execution, and then you included the Array UDF for its efficiency, and then without the actual data make assumptions about which method will take the fewest penalties....  I'm going to have a difficult time arguing the other side.

 

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

Link to comment
Share on other sites

keep calm.

I also don't know the real data for this problem and i clearly said that for small arrays nobody need to think about a efficient algorithm.
But if anyone with a similar problem reads this my statement is a hint for him that he should not wonder why the solution is really slow at bigger arrays.

Especially why we have to make assumptions about the actual data we should discuss in every possible direction.

Link to comment
Share on other sites

defeatist arguments are defeatist.  And "we have to make assumptions" might be their king.

While you are making assumptions should the OP explore MySQL when he gets through your UDF, because yours is slower?  My point was that the abstraction never ends, because everything about this language execution is slow af, comparatively.

imho, the best answer in a thread is the one that the OP could write and understand themselves with their current skill set.  Not necessarily the optimal solution.

 

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

Link to comment
Share on other sites

34 minutes ago, iamtheky said:

because everything about this language execution is slow af, comparatively.

Make a test with such an array with 300 Elements. With your solution the user have to wait 17s. With my 0.04s. And this discrepance increases exponential with the number of elements.
But i am not alowed to point at this behavoir because nobody use AutoIt for speed of execution - right?

Then thanks for the clarification of what i am allowed to talk about here.

Edited by AspirinJunkie
Link to comment
Share on other sites

Still making technical assumptions, and have now added social assumptions?  literally ended with "in my humble opinion" and you took that as directive?

And correct, nobody uses AutoIt in production for speed of execution (but I would like to hear a proper defense with a script that is faster than python, ruby, or bash alternatives).

Make a test thread (or use any of the 50 we have ran before) and we will get a litany of solutions all within seconds of eachother.  And even build edge cases where your flex sort will be slower, its not magic, nobody is doing anything special.

Edited by iamtheky
lost focus...as per usual

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

Link to comment
Share on other sites

  • Moderators

Ok, we have provided the OP with a couple of different ways to skin the cat - awesome. How about we leave it to the OP to decide which he wants to employ, rather than continuing the pointless argument?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Booooo.

I know the list sorting movie is the same every time, but I always begin hoping that maybe this time he doesn't shoot Old Yeller at the end and there's a badass sequel I didn't know about.

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

Link to comment
Share on other sites

8 hours ago, iamtheky said:

but I always begin hoping that maybe this time he doesn't shoot Old Yeller at the end and there's a badass sequel I didn't know about.

Then you should reconsider about your reaction on my simple hint about the behaviour on bigger numbers of elements.
The statement was correct and can be a helpfull hint for other users.
But everything you argue with was that a discussion of performance in AutoIt is senseless.

I say that is not: It is not a difference in only some hypothetic milliseconds - it's a difference from maybe 15s vs 0.05s and even more.
And this can be a difference of usable and unusable.
And that's why i really wonder about your reaction to people who point at this.

 

10 hours ago, iamtheky said:

but I would like to hear a proper defense with a script that is faster than python, ruby, or bash alternatives).

Even in C++ your approach performs worster with > 500 elements than Neutro's approach in AutoIt.
But better not talking about - right?

Link to comment
Share on other sites

Again, with feeling

story time ain't going to do it. Pick a data set and a sort objective and let the forum have at it.  And the last quote was language and interpreter as a whole, how much slower is method x in autoit vs method x in those other languages, any other comparison is non sequitir.

Am I supposed to do the Passive aggressive ending too?  also, for the OP, my solution runs faster, at what point or configuration does yours start winning?  (That's both rhetorical and dripping with sarcasm).

Edited by iamtheky

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...