Order Reals/Integers from least to greatest

Edward

New Member
Reaction score
2
I am looking for either an in-game function OR Jass script that can order real and integer variables from least to greatest.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Well, how do you mean, since you always have a set amount of arguments in Jass for a function...
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Can be done by array stack or linked list. But linked list is faster if the entered integer is at middle.
 

Edward

New Member
Reaction score
2
Well, how do you mean, since you always have a set amount of arguments in Jass for a function...

Here is an array of integers:

Array1 = {17, 4, -8, 0, 44, -1, 6}, cardinality = 7,
I want to order these from least to greatest into a new array of of the same cardinality, so:

Array2[1] = -8
Array2[2] = -1
Array2[3] = 0
Array2[4] = 4
Array2[5] = 6
Array2[6] = 17
Array2[7] = 44

I would imagine that it would look something similar to this, except that this doesn't work for some reason.

Set Y = 100,000

For Loop Integer B, 1 to 7 -----
-Loop for Integer A, 1 to 7-----
-Set X = Array1[A]
-If
X < Y
-Then
set Y = X
set Z = A
-Else
Do nothing (end loop for A)

-Set Array2 = Y
-Set Array1[Z] = 100,000 (end loop for B)

EDIT ----------------------
I finally got something to work, but it is very clumsy and it takes way too long. It requires 3 triggers and an extra trigger to check that it worked. I definitely need something better than this since you can NOT use this to accomplish a small task within in a much larger trigger (thus it is useless).

Trigger: 1
Trigger:
  • Initial
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set Integer1[1] = 5
      • Set Integer1[2] = 9
      • Set Integer1[3] = -10
      • Set Integer1[4] = 0
      • Set Integer1[5] = 2
      • Set IntegerV = 1


Trigger 2:
Trigger:
  • Order
    • Events
      • Time - Every 2.00 seconds of game time
    • Conditions
    • Actions
      • Set IntegerY = 10000
      • For each (Integer A) from 1 to 5, do (Actions)
        • Loop - Actions
          • Set IntegerX = Integer1[(Integer A)]
          • Set IntegerZ = (Integer A)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • IntegerX Less than IntegerY
            • Then - Actions
              • Set IntegerY = IntegerX
              • Set IntegerW = IntegerZ
            • Else - Actions
              • Do nothing
      • Set Integer2[IntegerV] = IntegerY
      • Set Integer1[IntegerW] = 10000
      • Set IntegerV = (IntegerV + 1)


Trigger 3
Trigger:
  • Order Stop
    • Events
      • Time - Elapsed game time is 11.00 seconds
    • Conditions
    • Actions
      • Trigger - Turn off Order &lt;gen&gt;


Trigger 4:
Trigger:
  • Display
    • Events
      • Time - Elapsed game time is 14.00 seconds
    • Conditions
    • Actions
      • Game - Display to (All players) for 3.00 seconds the text: (String(Integer2[1]))
      • Wait 3.00 game-time seconds
      • Game - Display to (All players) for 3.00 seconds the text: (String(Integer2[2]))
      • Wait 3.00 game-time seconds
      • Game - Display to (All players) for 3.00 seconds the text: (String(Integer2[3]))
      • Wait 3.00 game-time seconds
      • Game - Display to (All players) for 3.00 seconds the text: (String(Integer2[4]))
      • Wait 3.00 game-time seconds
      • Game - Display to (All players) for 3.00 seconds the text: (String(Integer2[5]))
      • Wait 3.00 game-time seconds
 

meOme

New Member
Reaction score
31
/edit:
After much fixing it works perfectly now.
You can use it multiple times during the map (in case Array1 changes over time), and it even works with multiple numbers, like ( 1, -4, 1, 3, 5, 1, -2 ).

You need these global variables:
Integer_Array1[]
Integer_Array2[]
Boolean_Array[] (initialized with "false")

Put this in your mapheader:
JASS:
function SortIt takes nothing returns nothing
    local integer a = 1
    local integer b
    local integer c
    local integer cardinality = 7

    loop
	exitwhen ( a &gt; cardinality )
	set udg_Integer_Array2[a] = 100000
	set b = 1
	set c = 0
	loop
	    exitwhen ( b &gt; cardinality )
	    if ( udg_Boolean_Array<b> == false and udg_Integer_Array1<b> &lt;= udg_Integer_Array2[a] ) then
		set udg_Integer_Array2[a] = udg_Integer_Array1<b>
		set udg_Boolean_Array<b> = true
		set udg_Boolean_Array[c] = false
		set c = b
	    endif
	    set b = b + 1
	endloop
	set a = a + 1
    endloop
    set a = 1
    loop
        exitwhen ( a &gt; cardinality )
        set udg_Boolean_Array[a] = false
        set a = a + 1
    endloop
endfunction</b></b></b></b>


Now you just need a simple Custom Script in your GUI-Code:
Trigger:
  • a
    • Events
      • Time - Elapsed game time is 0.00 seconds
    • Conditions
    • Actions
      • Set Integer_Array1[1] = -5
      • Set Integer_Array1[2] = 2
      • Set Integer_Array1[3] = 2
      • Set Integer_Array1[4] = -4
      • Set Integer_Array1[5] = 12
      • Set Integer_Array1[6] = 6
      • Set Integer_Array1[7] = 2
      • Custom script: call SortIt()
      • For each (Integer A) from 1 to 7, do (Actions)
        • Loop - Actions
          • Game - Display to (All players) the text: (String(Integer_Array2[(Integer A)]))

The result:
-5
-4
2
2
2
6
12

PS: It's alright if you don't understand perfectly how it works. I admit it's quite complicated. That's why I made something that you just have to copy&paste. Try to understand it if you like programming. xD
 

Edward

New Member
Reaction score
2
Nice bro that was awesome. I enjoy the challenge of initially doing what seems impossible.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top