Zahlen sortieren

  • Hallo alle zusammen,

    ich hoffe ihr könnt mir weiterhelfen, ich würde gerne Zahlen sortieren, diese sind nicht random.
    Ich weis leider nicht wie ich eine Sortierung hinbekomme, bspw Zahlen

    45
    2
    9
    413

    Wie kann ich die nun der größe nach sortieren? Habe bereits rumprobiert, kriege aber nichts zustande, was auch nur annähernd in die Richtung geht.
    Mit Arraysort geht es zwar, aber ich müsste die Daten in eine Datei speichern, nacheinander, das geht aber nicht, zumindest weist ich nicht wie ich diese aus dem Array rauskriege, so dass ich die Zahlen "aufschreiben" kann.

    Danke im Voraus!

  • Wie hast du die denn in das Array hinein bekommen wenn du schon ArraySort genutzt hast 8|

    Wtf. MAGIC

    Jetzt mal ernst, du sprichst diese einfach über ihren Index an: $Array[n]
    Ist als ob du den Wert von ner stink normalen Variable abfragst

    Gesendet von meinem HTC Desire HD A9191 mit Tapatalk 2

  • Von wo kommen die Daten her?

    [autoit]


    #include <Array.au3>

    [/autoit][autoit][/autoit][autoit]

    Local $sOutput = @ScriptDir & "\Output.txt"
    Local $aData[4] = [45,2,9,413];Hier deine Daten Quelle (in diesem Fall habe ich einfach ein Beispiel Array erstellt)

    [/autoit][autoit][/autoit][autoit]

    _ArraySort($aData)

    [/autoit][autoit][/autoit][autoit]

    For $i = 0 To UBound($aData) -1
    FileWriteLine($sOutput,$aData[$i])
    Next
    Exit

    [/autoit]

    :thumbup:

    • Offizieller Beitrag

    Wobei man auch ein Auge auf die Performance werfen sollte. In Loops nicht schreiben.

    [autoit]


    #include <Array.au3>

    [/autoit][autoit][/autoit][autoit]

    Local $sOutput = @ScriptDir & "\Output.txt"
    Local $aData[4] = [45,2,9,413];Hier deine Daten Quelle (in diesem Fall habe ich einfach ein Beispiel Array erstellt)

    [/autoit][autoit][/autoit][autoit]

    _ArraySort($aData)

    [/autoit][autoit][/autoit][autoit]

    Local $sWrite = ''
    For $i = 0 To UBound($aData) -1
    $sWrite &= $aData[$i] & @CRLF
    ; FileWriteLine($sOutput,$aData[$i])
    Next
    FileWrite($sOutput, $sWrite)
    Exit

    [/autoit]
  • Danke erstmal an alle!

    Habe mir das Beispiel von Bugfix genommen, funktioniert auch, sortiert aber leider falsch.

    Hier die Ausgabe:

    102
    102
    107
    110
    111
    115
    117
    12
    126
    126
    127
    143
    179
    180
    194
    220
    2230
    2720
    275
    2751
    276
    284
    288
    518
    52
    54
    558
    583
    88
    911

    gibt es eine Möglichkeit die ganze Zahl zu vergleichen? Nicht nur die Stellen? Es müsste wirklich genau die Reihenfolge sein, oder evtl. ein Workaround? Womit man dann nachträglich die Position verändert? =(
    Danke im Voraus!

    btw. Wie sortiere ich von groß nach klein? =(

    Einmal editiert, zuletzt von vism (20. November 2014 um 05:06)

  • gibt es eine Möglichkeit die ganze Zahl zu vergleichen? Nicht nur die Stellen?

    Das Problem bei dir ist, dass die Elemente in deinem Array nicht vom Datentyp Zahl (Int oder Float/Double) sind sondern eine Zeichenkette (ein "String"). Also nichts weiter als ein normaler Text. Bei Strings wird zum Größenvergleich zeichenweise vorgegangen. Daher diese Sortierung wie bei dir.
    Eine Lösung wäre alle Elemente im Array vorher durchzugehen und per Number()-Funktion in einen Zahldatentyp zu wandeln oder du zeigst einfach mal wie dein Array überhaupt entsteht.

    btw. Wie sortiere ich von groß nach klein? =(

    Hilfe zu _ArraySort() lesen.

    Einmal editiert, zuletzt von AspirinJunkie (20. November 2014 um 08:23)

  • Das Beispiel von BugFix funktioniert zu 100% mit deinen Zahlen von oben weil, die Datensätze im Array dort Integer sind egal welche Zahlen du da rein schreibst.
    Deshalb zeige uns bitte , wie AspirinJunkie schon gesagt hat, wo die du die Daten für das Array herholst und wie du die in das Array reinpackst.

    :thumbup:

  • Danke für die Antworten, so werden die Zahlen geholt:


    Spoiler anzeigen
    [autoit]

    For $j = 1 to 3 Step +1
    If $j = 1 Then
    $k = "total_kills_"
    ElseIf $j = 2 Then
    $k = "total_hits_"
    ElseIf $j = 3 Then
    $k = "total_shots_"
    EndIf
    ;knife
    $k_knife = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", "total_kills_knife", "") & "kni"
    ;pistols
    $k_glock = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "glock", "") & "glk"
    $k_deagle = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "deagle", "") & "dea"
    $k_elite = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "elite", "") & "eli"
    $k_fiveseven = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "fiveseven", "") & "fis"
    $k_hkp2000 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "hkp2000", "") & "hkp"
    ;mps
    $k_mac10 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "mac10", "") & "mac"
    $k_ump45 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "ump45", "") & "ump"
    $k_p90 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "p90", "") & "p90"
    $k_mp7 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "mp7", "") & "mp7"
    $k_mp9 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "mp9", "") & "mp9"
    $k_bizon = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "bizon", "") & "biz"
    $k_tec9 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "tec9", "") & "tec"
    $k_mag7 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "mag7", "") & "mag"
    ;rifles
    $k_aug = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "aug", "") & "aug"
    $k_famas = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "famas", "") & "fam"
    $k_g3sg1 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "g3sg1", "") & "g3s"
    $k_sg556 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "sg556", "") & "sg5"
    $k_scar20 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "scar20", "") & "sca"
    $k_ssg08 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "ssg08", "") & "ssg"
    $k_galil = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "galilar", "") & "gal"
    $k_m4a1 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "m4a1", "") & "m4a"
    $k_ak47 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "ak47", "") & "a47"
    $k_awp = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "AWP", "") & "awp"
    ;heavy
    $k_xm1014 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "xm1014", "") & "xm1"
    $k_m249 = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "m249", "") & "249"
    $k_nova = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "nova", "") & "nov"
    $k_negev = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "negev", "") & "neg"
    $k_sawedoff = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "sawedoff", "") & "saw"
    ;grenades
    $k_hegrenade = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", "total_kills_hegrenade", "") & "heg"
    $k_molotov = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", "total_kills_molotov", "") & "mol"
    ;misc
    If $k = 1 Or $k = 3 Then
    $k_taser = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", $k & "taser", "") & " = " & "tas"
    Else
    $k_taser = IniRead(@AppDataDir & "\csgoimg\statusreader\cfg.ini", "stats", "total_kills_taser", "") & "tas"
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Local $aData[32] = [$k_knife,$k_glock,$k_deagle,$k_elite,$k_fiveseven,$k_hkp2000,$k_mac10,$k_ump45,$k_p90,$k_mp7,$k_mp9,$k_bizon,$k_tec9,$k_mag7,$k_aug,$k_famas,$k_g3sg1,$k_sg556,$k_scar20,$k_ssg08,$k_galil,$k_m4a1,$k_ak47,$k_awp,$k_xm1014,$k_taser,$k_molotov,$k_hegrenade,$k_sawedoff,$k_negev,$k_nova,$k_m249];Hier deine Daten Quelle (in diesem Fall habe ich einfach ein Beispiel Array erstellt)

    [/autoit] [autoit][/autoit] [autoit]

    _ArraySort($aData)

    [/autoit] [autoit][/autoit] [autoit]

    Local $sWrite = ''
    For $i = 0 To UBound($aData) -1
    If $k = "total_hits_" Then
    $sWrite &= $k & "_" & StringRight($aData[$i], 3) & " = " & StringTrimRight($aData[$i], 3) & @CRLF
    Else
    $sWrite &= $k & StringRight($aData[$i], 3) & " = " & StringTrimRight($aData[$i], 3) & @CRLF
    EndIf
    ;~ FileWriteLine($sOutput,$aData[$i])
    Next
    FileWrite($sOutput, $sWrite)
    Next

    [/autoit]

    Die Datei aus der die Daten stammen, sieht so aus:

    Spoiler anzeigen

    [stats]
    total_kills=14753
    total_deaths=8499
    total_time_played=802337
    total_planted_bombs=532
    total_defused_bombs=155
    total_wins=6240
    total_damage_done=2121155
    total_money_earned=32251600
    total_rescued_hostages=511
    total_kills_knife=180
    total_kills_hegrenade=53
    total_kills_glock=521
    total_kills_deagle=560
    total_kills_elite=115
    total_kills_fiveseven=55
    total_kills_xm1014=220
    total_kills_mac10=110
    total_kills_ump45=289
    total_kills_p90=584
    total_kills_awp=2248
    total_kills_ak47=2733
    total_kills_aug=285
    total_kills_famas=182
    total_kills_g3sg1=104
    total_kills_m249=111
    total_kills_headshot=7170
    total_kills_enemy_weapon=873
    total_wins_pistolround=334
    total_wins_map_cs_assault=8
    total_wins_map_cs_italy=103
    total_wins_map_cs_office=129
    total_wins_map_de_aztec=105
    total_wins_map_de_cbble=30
    total_wins_map_de_dust2=627
    total_wins_map_de_dust=140
    total_wins_map_de_inferno=287
    total_wins_map_de_nuke=368
    total_wins_map_de_train=141
    total_weapons_donated=1522
    total_broken_windows=166
    total_kills_enemy_blinded=180
    total_kills_knife_fight=116
    total_kills_against_zoomed_sniper=949
    total_dominations=424
    total_domination_overkills=2434
    total_revenges=28
    total_shots_hit=46078
    total_shots_fired=217339
    total_rounds_played=10856
    total_shots_deagle=3241
    total_shots_glock=11095
    total_shots_elite=1077
    total_shots_fiveseven=453
    total_shots_awp=6069
    total_shots_ak47=58076
    total_shots_aug=3748
    total_shots_famas=2746
    total_shots_g3sg1=502
    total_shots_p90=11312
    total_shots_mac10=1797
    total_shots_ump45=3600
    total_shots_xm1014=4302
    total_shots_m249=1350
    total_hits_deagle=1031
    total_hits_glock=2537
    total_hits_elite=375
    total_hits_fiveseven=155
    total_hits_awp=2422
    total_hits_ak47=8723
    total_hits_aug=904
    total_hits_famas=659
    total_hits_g3sg1=177
    total_hits_p90=2653
    total_hits_mac10=614
    total_hits_ump45=1063
    total_hits_xm1014=1133
    total_hits_m249=304
    total_rounds_map_cs_assault=8
    total_rounds_map_cs_italy=119
    total_rounds_map_cs_office=173
    total_rounds_map_de_aztec=139
    total_rounds_map_de_cbble=76
    total_rounds_map_de_dust2=1145
    total_rounds_map_de_dust=158
    total_rounds_map_de_inferno=545
    total_rounds_map_de_nuke=670
    total_rounds_map_de_train=183
    last_match_t_wins=14
    last_match_ct_wins=16
    last_match_wins=15
    last_match_max_players=10
    last_match_kills=20
    last_match_deaths=25
    last_match_mvps=1
    last_match_favweapon_id=16
    last_match_favweapon_shots=202
    last_match_favweapon_hits=51
    last_match_favweapon_kills=13
    last_match_damage=2906
    last_match_money_spent=91600
    last_match_dominations=0
    last_match_revenges=0
    total_mvps=1976
    total_rounds_map_de_lake=24
    total_rounds_map_de_safehouse=37
    total_rounds_map_de_sugarcane=11
    total_rounds_map_de_stmarc=7
    total_rounds_map_de_bank=14
    total_rounds_map_de_shorttrain=4
    total_TR_planted_bombs=8
    total_TR_defused_bombs=8
    total_gun_game_rounds_won=135
    total_gun_game_rounds_played=150
    total_wins_map_de_house=5
    total_wins_map_de_bank=11
    total_wins_map_de_vertigo=8
    total_wins_map_ar_monastery=1
    total_rounds_map_ar_shoots=5
    total_rounds_map_ar_baggage=33
    total_wins_map_ar_shoots=5
    total_wins_map_ar_baggage=33
    total_wins_map_de_lake=24
    total_wins_map_de_sugarcane=11
    total_wins_map_de_stmarc=7
    total_matches_won_bank=1
    total_wins_map_de_shorttrain=5
    total_wins_map_de_safehouse=36
    total_matches_won=263
    total_matches_played=519
    total_gg_matches_won=39
    total_gg_matches_played=96
    total_progressive_matches_won=19
    total_trbomb_matches_won=20
    total_contribution_score=56849
    last_match_contribution_score=55
    last_match_rounds=30
    total_kills_hkp2000=916
    total_shots_hkp2000=14778
    total_hits_hkp2000=3691
    total_hits_p250=2064
    total_kills_p250=585
    total_shots_p250=9336
    total_kills_sg556=126
    total_shots_sg556=1271
    total_hits_sg556=313
    total_hits_scar20=223
    total_kills_scar20=143
    total_shots_scar20=596
    total_shots_ssg08=359
    total_hits_ssg08=169
    total_kills_ssg08=117
    total_shots_mp7=4425
    total_hits_mp7=1283
    total_kills_mp7=277
    total_kills_mp9=107
    total_shots_mp9=1756
    total_hits_mp9=621
    total_hits_nova=1062
    total_kills_nova=196
    total_shots_nova=5146
    total_hits_negev=367
    total_kills_negev=129
    total_shots_negev=2136
    total_shots_sawedoff=1839
    total_hits_sawedoff=314
    total_kills_sawedoff=72
    total_shots_bizon=4766
    total_hits_bizon=1490
    total_kills_bizon=276
    total_kills_tec9=126
    total_shots_tec9=767
    total_hits_tec9=258
    total_shots_mag7=2422
    total_hits_mag7=545
    total_kills_mag7=90
    total_gun_game_contribution_score=1478
    last_match_gg_contribution_score=0
    total_kills_m4a1=2784
    total_kills_galilar=346
    total_kills_molotov=102
    total_kills_taser=12
    total_shots_m4a1=53806
    total_shots_galilar=4543
    total_shots_taser=25
    total_hits_m4a1=10225
    total_hits_galilar=1082
    total_rounds_map_ar_monastery=2
    total_matches_won_train=2
    total_rounds_map_de_vertigo=8
    total_matches_won_shoots=5
    total_matches_won_baggage=8
    total_matches_won_lake=4
    total_matches_won_sugarcane=1
    total_matches_won_stmarc=6
    total_matches_won_safehouse=5
    total_matches_won_shorttrain=5

    Die Daten werden gefiltert, und landen dann so in einer neuen Datei:
    Hier sind die Daten sortiert, allerdings so wie oben schon erwähnt, werden die einzelnen Stellen verglichen, nicht die gesamten Zahlen. =/

    Spoiler anzeigen

    [status]
    total_kills_mol = 102
    total_kills_g3s = 104
    total_kills_mp9 = 107
    total_kills_mac = 110
    total_kills_249 = 111
    total_kills_eli = 115
    total_kills_ssg = 117
    total_kills_sg5 = 126
    total_kills_tec = 126
    total_kills_neg = 129
    total_kills_tas = 12
    total_kills_sca = 143
    total_kills_kni = 180
    total_kills_fam = 182
    total_kills_nov = 196
    total_kills_xm1 = 220
    total_kills_awp = 2248
    total_kills_a47 = 2733
    total_kills_biz = 276
    total_kills_mp7 = 277
    total_kills_m4a = 2784
    total_kills_aug = 285
    total_kills_ump = 289
    total_kills_gal = 346
    total_kills_glk = 521
    total_kills_heg = 53
    total_kills_fis = 55
    total_kills_dea = 560
    total_kills_p90 = 584
    total_kills_saw = 72
    total_kills_mag = 90
    total_kills_hkp = 916
    total_hits__m4a = 10225
    total_hits__mol = 102
    total_hits__dea = 1031
    total_hits__nov = 1062
    total_hits__ump = 1063
    total_hits__gal = 1082
    total_hits__xm1 = 1133
    total_hits__mp7 = 1283
    total_hits__tas = 12
    total_hits__biz = 1490
    total_hits__fis = 155
    total_hits__ssg = 169
    total_hits__g3s = 177
    total_hits__kni = 180
    total_hits__sca = 223
    total_hits__awp = 2422
    total_hits__glk = 2537
    total_hits__tec = 258
    total_hits__p90 = 2653
    total_hits__249 = 304
    total_hits__sg5 = 313
    total_hits__saw = 314
    total_hits__neg = 367
    total_hits__hkp = 3691
    total_hits__eli = 375
    total_hits__heg = 53
    total_hits__mag = 545
    total_hits__mac = 614
    total_hits__mp9 = 621
    total_hits__fam = 659
    total_hits__a47 = 8723
    total_hits__aug = 904
    total_shots_mol = 102
    total_shots_eli = 1077
    total_shots_glk = 11095
    total_shots_p90 = 11312
    total_shots_sg5 = 1271
    total_shots_tas = 12
    total_shots_249 = 1350
    total_shots_hkp = 14778
    total_shots_mp9 = 1756
    total_shots_mac = 1797
    total_shots_kni = 180
    total_shots_saw = 1839
    total_shots_neg = 2136
    total_shots_mag = 2422
    total_shots_fam = 2746
    total_shots_dea = 3241
    total_shots_ssg = 359
    total_shots_ump = 3600
    total_shots_aug = 3748
    total_shots_xm1 = 4302
    total_shots_mp7 = 4425
    total_shots_fis = 453
    total_shots_gal = 4543
    total_shots_biz = 4766
    total_shots_g3s = 502
    total_shots_nov = 5146
    total_shots_m4a = 53806
    total_shots_heg = 53
    total_shots_a47 = 58076
    total_shots_sca = 596
    total_shots_awp = 6069
    total_shots_tec = 767

  • Wie ich bereits sagte: Du musst den Datentyp noch ändern:

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    ; Ini-Section in Array einlesen
    $aEntries = IniReadSection("Test.ini", "stats")

    [/autoit] [autoit][/autoit] [autoit]

    ; Die Zahlen vom Datentyp String in Zahlentyp ändern:
    For $i = 1 To $aEntries[0][0]
    $aEntries[$i][1] = Number($aEntries[$i][1])
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ; Array nach zweiter Spalte sortieren:
    _ArraySort($aEntries, 0, 1,0, 1)

    [/autoit] [autoit][/autoit] [autoit]

    ; Datei schreiben:
    $hFile = FileOpen("Out.ini", 2)
    FileWriteLine($hFile, "[stats]")
    For $i = 1 To $aEntries[0][0]
    FileWriteLine($hFile, $aEntries[$i][0] & "=" & $aEntries[$i][1])
    Next
    FileClose($hFile)

    [/autoit]
  • Funktioniert so leider nicht,

    Spoiler anzeigen
    [autoit]

    ==> Subscript used on non-accessible variable.:
    For $i = 1 To $aEntries[0][0]
    For $i = 1 To $aEntries^ ERROR

    [/autoit]

    Wie deklariere ich das denn vorher? Habe mit IniReadSection leider noch nicht gearbeitet. =/

    EDIT: Hat sich erledigt. :D Hatte nen Rechtschreibfehler >_<, Danke!