Sorting an Array, and using it to filter another Array

So kinda having a brain fart here… How do I sort an Array, based on the Values of another Array?

Example:
Array A = A player List
Array B = Damage Values

Let’s say we have one player, who keeps track of the amount of damage they receive from all other players. When a player deals damage to this player, the attacker is added the Array A, and the amount of damage they deal is added (throughout the match) to Array B.

Now, once a player (the attacker) is added to Array A, not much else goes on with Array A again after that (other than adding a new player who has yet to deal damage to OUR main player here). It’s just a place holder, so the Damage values stored in Array B are in the same Index as Array A where the player names are stored.

Question:
If i were to have a HUD that displays the player who’s dealt the most damage to “this” player, how would i go about doing that?

This works…but it requires this to be to be looped through consistently to update the new values.

variables
{
	player:
		31: AttackerDamageValues
		38: AttackerNames
}

rule("Rule 44")
{
	event
	{
		Ongoing - Each Player;
		All;
		All;
	}

	conditions
	{
		Is Button Held(Event Player, Button(Interact)) == True;
	}

	actions
	{
		Event Player.A = Last Of(Sorted Array(Event Player.AttackerDamageValues, Current Array Element));
		Event Player.B = Index Of Array Value(Event Player.AttackerDamageValues, Event Player.A);
		Event Player.C = Event Player.AttackerNames[Event Player.B];
	}
}

I feel there should be some sort of Nested Array i can make, that’s all contained inside the HUD & simply set the HUD to reevaluate string values. For example:
Set player A
OR
Set Header = Last of. Sorted Array. Mapped Array (or Filtered Array??)

Where i somehow Sort the Damage Values Array to get the Highest value w/in that array, and then Map or Filter that value to the Player’s Name stored in the Attacker Name Array.

for the sorted value input do it like normal but instead of current array element do value in array([Target Array], current array index)

So, instead of;
Event Player.A = Last Of(Sorted Array(Event Player.AttackerDamageValues, Current Array Element
Do;
Event Player.A = Last Of(Sorted Array(Event Player.AttackerDamageValues, Current Array Index

This returned the same number. What’s the benefit of doing it this way? Or did i miss understand what you’re saying?

I’m sure he means a dictionary keys-values retrieval the syntax is just this in your case :

actions
{
	Global.C = Last Of(Sorted Array(Global.A, Global.B[Current Array Index]));
}
1 Like

the sorted array snippet that ArcanaXXI gave, was the intended solution that i mentioned. you missed the Value in Array part of what i said. also in case its not clear to you, they write their snippet in the actual code or copied it directly from the workshop. which is why it doesn’t contain the value in array inside it. since thats the [] part. if you confused just paste what they said into the workshop to view it.

2 Likes

Thanks yes and here is an example code:

variables
{
	player:
		0: Damager
		1: Values
		2: HUDs
}

rule("Rule 1")
{
	event
	{
		Ongoing - Each Player;
		All;
		All;
	}

	actions
	{
		"List of potential damagers/attackers"
		Event Player.Damager = Array();
		"List of damage values caused by the corresponding damager/attacker listed in Damager array."
		Event Player.Values = Array();
		"No comment"
		Event Player.HUDs = Array();
		"Display the damager's/attacker's name who did the most damage to someone."
		Create HUD Text(Event Player, Custom String("{0} dealt the most damage", Last Of(Sorted Array(Event Player.Damager,
			Event Player.Values[Current Array Index]))), Null, Null, Left, 0, Color(White), Color(White), Color(White),
			Visible To and String, Default Visibility);
		"Reference to the hud or huds."
		Event Player.HUDs[0] = Last Text ID;
	}
}

rule("Rule 2")
{
	event
	{
		Player Took Damage;
		All;
		All;
	}

	conditions
	{
		Victim != Attacker;
	}

	actions
	{
		"Sets the damagers name at specified index position which is the slot of the damager."
		Victim.Damager[Slot Of(Attacker)] = Custom String("{0}", Attacker);
		"Aggregate damage values."
		Victim.Values[Index Of Array Value(Victim.Damager, Custom String("{0}", Attacker))] += Event Damage;
	}
}

If you want it to handle and obtain the highest damage dealer in descending order start with First Of and multiply the Current Array Index with -1. So in the context of sorting the array values they got flipped around. For more information have a look here
[Reverse an array | Wiki | Workshop.codes](HTTP S://workshop.codes/wiki/articles/reverse+an+array)

1 Like

Thank you so much ArcanaXXI! I see now what I was doing wrong with the Sorted Array, specifically the Value Rank part. Also, really appreciate the Tip on how to swap [First Of] & [Last Of] by multiplying by -1 (If the need ever arises.)

& thanks to everyone else who chimed in. Nice to see a community so willing to help. Cheers!

No problem, glad i could help even though i am not that active in resolving issues provided in the forums anymore, if i have the desire to i will try my best to help :slight_smile: .

1 Like