Jump to content
Sign in to follow this  
mschol

sorting ini key's based on value

Recommended Posts

mschol

i have an ini like this:

[time]
1=timeticks1
2=timeticks2
3=timeticks3

[section2]
1=value1
2=value2
3=value3

[section3]
1=value1
2=value2
3=value3

what i want is to sort every key, in the "times" section, from high to low (so 1 is biggest, 3 is smallest)

the time keys contain timeticks so just a long number :D

but then the tricky part, the other sections must also be sorted, exactly as the key's in the time section..

any idea's on how to do that?

hope you can help (and understand what i mean :P)

Share this post


Link to post
Share on other sites
weaponx

So do you want to sort by key name or by value?

Do you need the section names sorted too?

Edited by weaponx

Share this post


Link to post
Share on other sites
weaponx

This will sort every section in the file by value:

#include <array.au3>

$ini = "test.ini"
$tempini = "temp.ini"

;Collect all section names
$sectionNames = IniReadSectionNames($ini)

;Delete temp file
FileDelete($tempini)

;Sort all sections in file
For $X = 1 to $sectionNames[0]
    
    ;Collect all key/value pairs
    $sectionData = IniReadSection($ini, $sectionNames[$X])

    ;Sort Descending
    ;Start at element 1
    ;Default ubound
    ;2 occurences in second dimension
    ;Sort on index 1
    _ArraySort ($sectionData,1, 1, 0, 2, 1)
    
    ;Write to temp file
    For $Y = 1 to $sectionData[0][0]
        IniWrite($tempini,$sectionNames[$X],$sectionData[$Y][0], $sectionData[$Y][1])
    Next
Next

;Replace original file
;FileMove($tempini, $ini, 1)

;Delete temp file
;FileDelete($tempini)

test.ini:

[section3]
3=30
2=20
1=10

[section2]
3=10
2=20
1=30

[section1]
3=20
2=10
1=30

Share this post


Link to post
Share on other sites
mschol

hmm lets explain it with some real data:

[times]

1=21545000

2=17944000

[village]

1=[01] (531|130) K15

2=[07] (537|131) K15

[units]

1=0;0;1000;0;0;300;0;0;150;0;0;0

2=0;34;0;0;0;20;0;0;14;0;3;0

[catapult]

1=farm

2=mine

[destination]

1=529|106

2=521|104

from the example:

key number 2 needs to be the first key instead of the last (key 2 has a lower value)

but the other number 2 key's, in the other sections, also need to be at the good place (thus also move to number 1 in this example)

this might be easy for the example but the real file can have over 10 key's per section which need to be sorted :D

and still a nice script btw :P

Edited by mschol

Share this post


Link to post
Share on other sites
weaponx

So are you saying whatever order the [times] section keys are (after sorting), the rest of the sections need to be in the same order?

Edited by weaponx

Share this post


Link to post
Share on other sites
Richard Robertson

Why do you need to sort INI data? Unless you mean that you want lowest to be key 1, next key 2, etc, but then you really should have just written the data to the file like that in the first place.

Is the file so big that you can't do this by hand?

Share this post


Link to post
Share on other sites
mschol

yep indeed,

and sure i'm able to sort it by hand but if i have an ini with +10 keys in each section, i will sure do something wrong which ends up that the result of the script wil be wrong

and my idea is that i am able to just add items (through an gui) unsorted and sort it afterwards.

FYI: it is a script to launch a noble attack in a webbased game, and from each village you have a certain travelling time.. (to another village)

what it does:

1) look with a while loop at the times section

while 1
    if $i <= $result[0][0] Then
        if _TimeToTicks(@HOUR,@MIN,@SEC) == $result[$i][1] Then
            attack($i)
            $i+= 1
        EndIf
    Else
        Exit
    EndIf
WEnd

and execute a function (which reads the other sections)

but to have more attacks at the same time the times must be sorted from low to high

Share this post


Link to post
Share on other sites
GEOSoft

Here's a UDF that should do what you want.

http://dundats.mvps.org/AutoIt/files/Ini.zip

EDIT: If this is not what you want I could always add an _Ini_SortData() function to sort based on the Key Value instead of on the key name

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
GEOSoft

Here's a UDF that should do what you want.

http://dundats.mvps.org/AutoIt/files/Ini.zip

EDIT: If this is not what you want I could always add an _Ini_SortData() function to sort based on the Key Value instead of on the key name

I've now added this functionality to the UDF

Download

Ini.zip


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.