Jump to content

Probs with sorting Array


pille24
 Share

Recommended Posts

Hi guys...

I want to sort an array in descending order...the problem is that the numbers contain decimal points, for example the numbers are sumthing like 5.5;6.2 etc..

Now it doesnt work at all, if i am using _ArraySort($array)...

Does someone know why?

And then i just remember;), another prob...

The values from the array i read from an ini-file. In every section there is one value which is put into the array.

I want all the sections in decreasing order of their one value. The prob is that each section contains other values, and now i dont know how to output them into an 'Input-Field'?

Example:

Ini File:

[1]

value=6.2

othervalue=spare

[2]

value=5.5

othervalue=spare2

Now i wanted first the array to be sorted, and then to be output in a table:

Othervalue Value

spare 6.2

spare2 5.5

if you dont understand the question, ask...

Thx for help

Link to comment
Share on other sites

Hi guys...

I want to sort an array in descending order...the problem is that the numbers contain decimal points, for example the numbers are sumthing like 5.5;6.2 etc..

Now it doesnt work at all, if i am using _ArraySort($array)...

Does someone know why?

And then i just remember;), another prob...

The values from the array i read from an ini-file. In every section there is one value which is put into the array.

I want all the sections in decreasing order of their one value. The prob is that each section contains other values, and now i dont know how to output them into an 'Input-Field'?

Example:

Ini File:

[1]

value=6.2

othervalue=spare

[2]

value=5.5

othervalue=spare2

Now i wanted first the array to be sorted, and then to be output in a table:

Othervalue Value

spare 6.2

spare2 5.5

if you dont understand the question, ask...

Thx for help

can you post your sort code? I'll post some sorts you can use too, as soon as i find them...
Link to comment
Share on other sites

can you post your sort code? I'll post some sorts you can use too, as soon as i find them...

here's a good one i use alot. It's called a shell sort. This assumes array name is $array and a variable $n which is equal to the count of items in the array.

Dim $inner,$outer,$temp; declare variables
$h = 1
While $h <= $n/3
    $h = $h * 3 + 1
WEnd

While $h >0
    For $outer = $h To $n-1
        $temp = Number(StringStripWS($array[$outer],1))
        $inner = $outer
        While $inner > ($h - 1) And Number(StringStripWS($array[$inner-$h],1)) >= Number($temp)
            $array[$inner] = $array[$inner-$h]
            $inner = $inner - $h
        WEnd
        $array[$inner] = $temp
    Next
$h = ($h - 1)/3
WEnd
Link to comment
Share on other sites

I just used _arraysort

I actually dont understand your example, but Ill try later...thx

you don't understand the usage or the algorithm? Usage is simple, just change your array name to $array, then add the lines :

$n = $array[0]
_ArrayDelete($array,0)

then paste my code below that.

once that code finishes, $array will be a sorted array. the algorithm is a little tougher to explain. Typically the part of a sort that takes the most time, is the actual moving of elements. this sort reduces the number of necessary moves by examining the array in pieces. lets say you have an array of 30 elements. the first thing it does, is figure out how big of a step to use on the first pass, which is usually somewhere around 1/3 the total number of elements.... after 3 iterations of the while loop, the first step is determined to be 13. So every 13th element is put into order, a total of 4 values may be moved around, so even if they have to be moved to opposite ends from their starting positions, it's pretty fast. Then the step is reduced by 1, and divided by 3, creating a new step of 4. The same thing happens again, with every 4th item being put into order. once that iteration completes, every value is within 4 positions of it's final position. on the next pass, even though each value has to be compared, and moved if it's out of order, none of them have to be moved very far, with an average of 2 places moved per item. That is perhaps an overly simplified explanation of the algorithm, but hopefully you get the idea. i compared the total sort times of 4 different search algorithms with an array of 10,000 random numbers (the numbers were generated at random, but the same numbers were used for each sort), with the following results:

bubble sort averaged 43.63 seconds to put all of the numbers in order

selection sort averaged 23.05

insertion sort averaged 17.27

Shell sort destroyed the others, coming in at 1.62 second average for the same 10,000 items.

Link to comment
Share on other sites

I think i got an easier way to sort my data. But however i need still you help:D

I could store my data in a *.csv file.

And now I want to show the data like in Excel, is that possible?

you could just display it IN excel... why don't you give us a bigger picture of what exactly you're tyring to accomplish and we can definitely help
Link to comment
Share on other sites

you could just display it IN excel... why don't you give us a bigger picture of what exactly you're tyring to accomplish and we can definitely help

k, im doing a screenshot....

Posted Image

thats my *.ini file. I want to sort the data according to 'gesamt' (=total)

Posted Image

It should look so, its not an Excel file, bit from another programm, you can sort just by clicking on the headings.

Posted Image

and so it does look...

I hope you got a better expression of my prob now

Link to comment
Share on other sites

Func sortieren()
$file="config.ini"
$kader=_FileCountLines( $file )/7
Global $array[$kader+5]
For $i=1 to $kader
    $array[$i]=IniRead ( $file, $i, "Gesamt", "0" )
Next    

$n=$array[0]
_arraydelete($array,0)

Dim $inner,$outer,$temp; declare variables
$h = 1
While $h <= $n/3
    $h = $h * 3 + 1
WEnd

While $h >0
    For $outer = $h To $n-1
        $temp = Number(StringStripWS($array[$outer],1))
        $inner = $outer
        While $inner > ($h - 1) And Number(StringStripWS($array[$inner-$h],1)) >= Number($temp)
            $array[$inner] = $array[$inner-$h]
            $inner = $inner - $h
        WEnd
        $array[$inner] = $temp
    Next
$h = ($h - 1)/3
WEnd

EndFunc

Thats now my code....

unfortunately it does not really work...

here is my ini file:

[1]
pos=T
name=Piffu Kontora 
st=2
en=13
fm=18
gesamt=6.2
[2]
pos=T
name=Christian Kurz
st=1
en=15
fm=20
gesamt=3.5
[3]
pos=A
name=Adman Busch
st=2
en=13
fm=8
gesamt=4.2
[4]
pos=A
name=Klaus Samurat
st=1
en=6
fm=13
gesamt=1.9
[5]
pos=A
name=Salvatone Pieato
st=1
en=16
fm=16
gesamt=3.2
[6]
pos=A
name=Graman Köpke
st=1
en=11
fm=18
gesamt=2.1
[7]
pos=A
name=Eric Hoyzer
st=1
en=3
fm=18
gesamt=2.1
[8]
pos=A
name=Bert Mazafaka
st=1
en=16
fm=19
gesamt=3.5
[9]
pos=M
name=Klaus Wiener
st=2
en=10
fm=18
gesamt=5.6
[10]
pos=M
name=Hunico Hund
st=1
en=16
fm=19
gesamt=3.5
[11]
pos=M
name=Erman Hund
st=1
en=14
fm=17
gesamt=3.1
[12]
pos=M
name=Garuin Grönemeyer
st=1
en=5
fm=16
gesamt=2.1
[13]
pos=M
name=Hildher Malik
st=1
en=3
fm=16
gesamt=1.9
[14]
pos=M
name=Alfhard Berthold
st=1
en=3
fm=13
gesamt=1.6
[15]
pos=S
name=Stayat Flick
st=2
en=6
fm=13
gesamt=3.8
[16]
pos=S
name=Marvon Goldberg
st=2
en=13
fm=11
gesamt=4.8
[17]
pos=S
name=Alfheri Stephens
st=1
en=17
fm=20
gesamt=3.7
[18]
pos=S
name=Hagihar Gerbertsheim
st=1
en=20
fm=20
gesamt=4

Do you know where the mistake is?

Link to comment
Share on other sites

  • Developers

what about :

#include<array.au3>
$file="config.ini"
Global $array[1][2]
While 1
    $i = UBound($array)
    $Temp=IniRead ($file, $i, "Gesamt", "??????")
    If $temp = "??????" Then
        ExitLoop
    EndIf
    Redim $array[UBound($array)+1][2]
    $array[$i][0]=$i
    $array[$i][1]=$Temp
Wend    
_ArraySort($array,1,1,0,2,1)
For $x = 1 to UBound($array) -1
;MsgBox(0,"demo", " Place:" & $x & '   Original place:' & $array[$x][0] & '  Gesamt:' & $array[$x][1])
    ConsoleWrite(" Place:" & $x & '   Original place:' & $array[$x][0] & '  Gesamt:' & $array[$x][1] & @lf)
Next

EDIT Fixed logic..

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

what about :

#include<array.au3>
$file="config.ini"
Global $array[1][2]
While 1
    $i = UBound($array)
    $Temp=IniRead ($file, $i, "Gesamt", "??????")
    If $temp = "??????" Then
        ExitLoop
    EndIf
    Redim $array[UBound($array)+1][2]
    $array[$i][0]=$i
    $array[$i][1]=$Temp
Wend    
_ArraySort($array,1,1,0,2,1)
For $x = 1 to UBound($array) -1
;MsgBox(0,"demo", " Place:" & $x & '   Original place:' & $array[$x][0] & '  Gesamt:' & $array[$x][1])
    ConsoleWrite(" Place:" & $x & '   Original place:' & $array[$x][0] & '  Gesamt:' & $array[$x][1] & @lf)
Next

EDIT Fixed logic..

cheers dude!

That does work perfectly....

im now wondering how to set the data the easiest way into the fields...

Link to comment
Share on other sites

  • Developers

cheers dude!

That does work perfectly....

im now wondering how to set the data the easiest way into the fields...

The $array[$x][0] contains the original INI value so you can read them all from the INI and then do whatever you want to do .... Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The $array[$x][0] contains the original INI value so you can read them all from the INI and then do whatever you want to do ....

well, i just need for example the name, but in the right order, and i dont know how to sort them, so that they match with theother value...

i tried it with a for..next loop:

If $array[$x][1]=iniRead($file,$x,"name","whatever") then
blabalbablabl rest of code

But it only sets the first three names, however on the right places.

Link to comment
Share on other sites

  • Developers

well, i just need for example the name, but in the right order, and i dont know how to sort them, so that they match with theother value...

But it only sets the first three names, however on the right places.

Just like this ?

#include<array.au3>
$file="config.ini"
Global $array[1][2]
While 1
    $i = UBound($array)
    $Temp=IniRead ($file, $i, "Gesamt", "??????")
    If $temp = "??????" Then
        ExitLoop
    EndIf
    Redim $array[UBound($array)+1][2]
    $array[$i][0]=$i
    $array[$i][1]=$Temp
Wend    
_ArraySort($array,1,1,0,2,1)
For $x = 1 to UBound($array) -1
;MsgBox(0,"demo", " Place:" & $x & '   Original place:' & $array[$x][0] & '  Gesamt:' & $array[$x][1])
    ConsoleWrite(" Place:" & $x & '   Original place:' & $array[$x][0] & '  Gesamt:' & $array[$x][1] & '  Name:' & IniRead ($file, $array[$x][0], "name", "??????") & @lf)
Next

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I found a 'bug'

If the values has not got the same amount of digits, it doesnt work, eg if one value is 20.5 and one 6.3, it thinks the last one is bigger. If I put a 0 before the 6.3, it works.

But how can i do it without putting a 0 in front?

Link to comment
Share on other sites

  • Developers

I found a 'bug'

If the values has not got the same amount of digits, it doesn't work, eg if one value is 20.5 and one 6.3, it thinks the last one is bigger. If I put a 0 before the 6.3, it works.

But how can i do it without putting a 0 in front?

Nah ... works as designed... it is sorting alphanumeric.

change this one line to:

$array[$i][1]=number($Temp)

and it will sort numerically...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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