Getting the Index number of lowest 7 ints in list
I have a case where a certain amount of shots are used in each round and put into an int. I have several rounds and store them in a list. I want to know the index values of the 7 lowest ints in the list so i can find the rounds where the least amount of shots were done. I have looked at doing something like shots.FindIndex
but i cant make it work. Any help would be appreciated
Code:
public List<int> shots = new List<int>();
public int bestScore;
// Use this for initialization
void Start()
{
hits.Add(0);
hits.Add(1);
hits.Add(2);
hits.Add(3);
hits.Add(4);
hits.Add(5);
hits.Add(6);
hits.Add(7);
hits.Add(8);
}
So what you are trying to do is go through the list and find the one with the lowest amount of shots?
Answer by KittenSnipes · Jan 27, 2018 at 12:14 AM
I think this is what you are going for. I typed it on my phone so I am not absolutely sure it will do what I think so it is a bit untested:
public List<int> shots;
public int bestScore;
// Use this for initialization
void Start()
{
shots = new List<int>();
hits.Add(0);
hits.Add(1);
hits.Add(2);
hits.Add(3);
hits.Add(4);
hits.Add(5);
hits.Add(6);
hits.Add(7);
hits.Add(8);
}
int Min() {
int lowestShots = INT_MAX;
for (int i = 0; i < hits.Count; i++ ) {
if (lowestShots < hits[i]) {
lowestsShots = hits[i];
}
return lowestsShots;
}
}
void Update() {
int lowestAmountOfShots;
lowestAmountOfShots = Min();
}
Sorry for the late answer, but i cant seem to make it work... I set the lowestShots variable to a number that is high enough to make sure no index on the list is bigger. The variable i get from the function is just the number i declare lowestShot as before the for loop
Here is the code:
public List<int> hits = new List<int>();
public int bestScore = 0;
// Use this for initialization
void Start()
{
hits.Add(0);
hits.Add(1);
hits.Add(2);
hits.Add(3);
hits.Add(4);
hits.Add(5);
hits.Add(6);
hits.Add(7);
hits.Add(8);
bestScore = $$anonymous$$in();
print(hits[8]);
}
int $$anonymous$$in()
{
int lowestShots = 10000000;
for (int i = 0; i < hits.Count; i++)
{
if (lowestShots < hits[i])
{
lowestShots = hits[i];
}
}
return lowestShots;
}
Inside print ins$$anonymous$$d of putting hits[8] put bestScore
Oh... i didnt revert that... that was just me trying some stuff. I still get the lowestShot value
Set lowestShots = 0 not that high number. Otherwise it will never work
Answer by Alanisaac · Jan 28, 2018 at 09:16 PM
Another way to do this would be to use LINQ. You can use anonymous objects to make this a little easier:
hits.Select((x, i) => new { Shots = x, Index = i })
.OrderBy(x => x.Shots)
.Take(7)
.Select(x => x.Index);
How do i make LINQ work in unity? i have seen other people talk about it when it comes to lists before
Does it return a list and if so, how do the syntax work?
This returns an enumerable, which is essentially something you can use in a foreach loop. I put this into the context of your script above. Your original post mentioned that you wanted not just the lowest index, but the seven lowest indexes. You can use this script to get any number of lowest indexes you want:
public List<int> hits = new List<int>();
public List<int> shots = new List<int>();
public int bestScore = 0;
// Use this for initialization
void Start()
{
hits.Add(15);
hits.Add(2);
hits.Add(56);
hits.Add(24);
hits.Add(6);
hits.Add(74);
hits.Add(42);
hits.Add(52);
hits.Add(42);
var threeLowestIndexes = LowestIndexes(3);
foreach(var index in threeLowestIndexes)
{
print(index);
// you should see:
// 1
// 4
// 0
// which are the indexes in the above list corresponding to 2, 6, 15.
}
}
IEnumerable<int> LowestIndexes(int count)
{
return hits.Select((x, i) => new { Shots = x, Index = i })
.OrderBy(x => x.Shots)
.Take(count)
.Select(x => x.Index);
}
Answer by victorbisaev · Jan 28, 2018 at 09:52 PM
Create a list of records of type ("number of hits", "index in the array"), fill it with actual values based on "hits" list, then sort the list (by "number of hits") and use items with indices 0..6, extracting corresponding "index in the array" from the sorted list and use it as an index into "hits" like "hits[sortedList[0].indexInHits]". LINQ above perform the same procedure but there are some concerns about using LINQ in Unity (check https://forum.unity.com/threads/to-linq-or-not-to-linq.223887/)
Your answer
Follow this Question
Related Questions
I need help finding the index of an object in a list. 1 Answer
Trying to index a list, gives error. 1 Answer
[ANSWERED] IndexOf and LastIndexOf both returning -1 when an item is on the list. 1 Answer
How to search a LIST(not an array) for game objects with a specific tag? 1 Answer
How I randomise a list of signals in Unity and remove it from the list once the clip is finished? 0 Answers