- Home /
Find the highest first value of a vector2 in a list?
I have a list of vector2's:
List playerSpiele = new List(4);
This is where the List is filled:
playerSpiele[playerNumber - 1] = new Vector2(game, suit);
I want to find the Vector2 with the highest value of the int "game" and return its index, if there are two of the same value, i want both of their indexes.
I can't think of a way to do this, can anyone help me with it? Thanks in advance!
Answer by unidad2pete · Aug 22, 2017 at 11:43 PM
You only need a loop to compare each value of your list.
List<Vector2> List = new List<Vector2>(4) { };
float x = 0;
float y = 0;
int indexX = 0;
int indexY = 0;
bool allValues0 = true;
for(int i = 0; i < List.Count; i++)
{
if(List[i].x > x)
{
x = List[i].x;
indexX = i;
allValues0 = false;
}
if (List[i].y > y)
{
y = List[i].y;
indexY = i;
allValues0 = false;
}
}
if(allValues0)
{
print(" All values are 0");
} else
{
print("the biggest X is = " + x + " on Index = " + indexX);
print("the biggest Y is = " + y + " on Index = " + indexY);
}
That does not seem to fully answer the question, OP asks for all indexes of the highest value. This only returns the first iteration.
You right, but I think the question is how do it, and the ansewer is a loop to find all values of a list.
But dont worry:
public List<Vector2> List;
public List<int> indexesX;
public List<int> indexesY;
private void Start()
{
List = new List<Vector2>(4) { };
indexesX = new List<int>();
indexesY = new List<int>();
}
private void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
float x = 0;
float y = 0;
indexesX.Clear();
indexesY.Clear();
bool allValuesX0 = true;
bool allValuesY0 = true;
for (int i = 0; i < List.Count; i++)
{
if (List[i].x >= x && List[i].x != 0)
{
if (List[i].x == x)
{
indexesX.Add(i);
}
else
{
x = List[i].x;
indexesX.Clear();
indexesX.Add(i);
}
allValuesX0 = false;
}
if (List[i].y >= x && List[i].y != 0)
{
if (List[i].y == y)
{
indexesX.Add(i);
}
else
{
y = List[i].y;
indexesY.Clear();
indexesY.Add(i);
}
allValuesY0 = false;
}
}
if (allValuesX0)
{
print(" All values of X are 0");
}
else
{
print("the biggest X is = " + x + " on Indexes ");
foreach (int index in indexesX)
{
print(index);
}
}
if (allValuesY0)
{
print(" All values of Y are 0");
}
else
{
print("the biggest Y is = " + y + " on Indexes ");
foreach (int index in indexesY)
{
print(index);
}
}
}
}
Answer by Xarbrough · Aug 22, 2017 at 11:50 PM
Here is my take:
public class Data
{
public int game;
public int suit;
}
List<Data> games = new List<Data>();
private List<int> FindHighestGames(List<Data> input)
{
List<int> results = new List<int>(); // Better us HashSet<int> and cache/recycle it.
// Find the highest score.
int highestGame = -1; // some value, that should never appear in Data.game
for (int i = 0; i < input.Count; i++)
{
if (input[i].game > highestGame)
highestGame = input[i].game;
}
// Now find all with the highest score and add them.
for (int i = 0; i < input.Count; i++)
{
if (input[i].game == highestGame)
results.Add(i); // store index.
}
return results;
}
Things to note:
You shouldn't represent score with floats. When you're comparing values, better use ints. You can always convert them to the desired output format later. Comparing floats might cause subtle trouble, for example when checking if they are equals, because of floating-point imprecision.
If I understood what you wanted correctly, the algorithm does this: Search for the highest score from a list of inputs. Then checks to see if there are other scores which are the same and returns the indices of them all in a collection.
Sorry, I'm new to coding. How would i convert my Vector2 to Data?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to read X and Y component ( of a vector2) from a list 1 Answer
Multiple Cars not working 1 Answer
How to make an IList of a certain type? 2 Answers
Distribute terrain in zones 3 Answers