How can i compare the current item in list with the next item ?

In this method i want first to check the distances.
For example in the List pairList in index 0,1,2 all the distances are the same all of them are 10.
So i want to add them to a List samedistances.

Then to continue loop over the pairList until the next part where distances will be the same and add them also to the samedistances list.

So in the end the samedistances list should be look something like:

10
10
10
20
20
44
44
44
44
50
50

But the way it is now it’s adding first only two distances of 10 but there are three.
Then it’s adding one distance of 14.43455 but there is only one of that distance.

And the first distance is 0 so not need to compare it at all.

And last in the end i’m getting exception ArgumentOutOfRangeException: Argument is out of range since i’m doing i+1

private void MoveInPath()
    {
        List<float> samedistances = new List<float>();

        for (int i = 0; i < pairList.Count; i++)
        {
            if (pairList[i].Distance == pairList[i+1].Distance)
            {
                samedistances.Add(pairList[i].Distance);
            }
        }
    }

So you want a copy of pairlist but sorted? Just use YourList.Sort(Comparer.default) on the copy.

var samedistanceList = new List<float>(pairlist);
samedistanceList.Sort(Comparer<float>.default);

I can’t
I forgot how i built the pairList.

In the top of the script i added:

[System.Serializable]
    public class PosDisPair 
    {
        public float Distance;
        public Vector3 Pos;
    }

Then

private List<PosDisPair> pairList = new List<PosDisPair>();

then

private void FindDistances()
    {
        pairList = new List<PosDisPair>();

        for (int i = 0; i < objects.Count; i++)
        {
            PosDisPair pairToAdd = new PosDisPair();
            pairToAdd.Distance = Vector3.Distance(EndStartPoints[0].transform.position, objects[i].transform.position);
            pairToAdd.Pos = objects[i].transform.position;
            pairList.Add(pairToAdd);
        }

        pairList.Sort(delegate (PosDisPair a, PosDisPair b) {
            return (a.Distance.CompareTo(b.Distance));
        });

        TestDis();
    }

And

private void TestDis()
    {
        List<float> samedistances = new List<float>();

        pairList.Sort(Comparer<float>.Default);

        for (int i = 0; i < pairList.Count; i++)
        {
            if (pairList[i].Distance == pairList[i+1].Distance)
            {
                samedistances.Add(pairList[i].Distance);
            }
        }
    }

Now i tried the line:

pairList.Sort(Comparer<float>.Default);

But getting error:

cannot convert from ‘System.Collections.Generic.Comparer’ to ‘System.Collections.Generic.IComparer<LevelGenerator.PosDisPair>’

What is not working with your version with the delegate? it looks correct at a first glance

private void FindDistances()
    {
        pairList = new List<PosDisPair>();
        for (int i = 0; i < objects.Count; i++)
        {
            PosDisPair pairToAdd = new PosDisPair();
            pairToAdd.Distance = Vector3.Distance(EndStartPoints[0].transform.position, objects[i].transform.position);
            pairToAdd.Pos = objects[i].transform.position;
            pairList.Add(pairToAdd);
        }
       
          pairList= pairList.OrderBy(x => x.Distance).ToList();
        });
        TestDis();
    }
pairList= pairList.OrderBy(x => x.Distance).ToList();

Will require
using System.Linq;
at the top, but should do what you want.

This is working but i will explain why i tried to use the way i did it before:

for (int i = 0; i < pairList.Count; i++)
        {
            if (pairList[i].Distance == pairList[i+1].Distance)
            {
                samedistances.Add(pairList[i].Distance);
            }
        }

The reason is that i want to get random distance item out of same distances.
For example if there three 10 distances then pic random one of them.
The distances are the same three same 10 distances but each one have another position.

pairList*.Position*
So what i want is each group of same distances to pick one random.
So for example there three distances of 10:
Distance 10 - Position 1,1,1
Distance 10 - Position 1,23,45
Distance 10 - Position 1,2,2
So i want to pick a random distance and then use the position of this distance.

Well, you’re not checking your List’s bounds while you interate/index (it).
You’re comparing index ‘i’ with ‘i+1’ (while the loop is < …Count), which means the last entry will always be out of bounds, at least.
To expand on your design goal for wanting to compare as many that are the same, I’d say you could do an inner loop , maybe like:

List<List<int>> samelist = new List<List<int>>();
List<int> num = new List<int>();
num.Add(5);
num.Add(5);
num.Add(7);
            num.Add(7);
            num.Add(7);
            num.Add(7);
            num.Add(10);
            num.Add(20);
            num.Add(74);
            num.Add(74);
            num.Add(2);
            num.Add(98);
            num.Add(98);
            num.Add(98);
            num.Add(98);
            num.Add(98);
            int lastmatch = -1;
            int b = num.Count - 1;
            for(int i = 0; i < num.Count; ++i){
              if(i < b && num[i] == num[i+1]) {
                  samelist.Add(new List<int>());
                  samelist[samelist.Count-1].Add(num[i]);
                  samelist[samelist.Count-1].Add(num[i]);
                 int j = i+2;
                 while(j <= b) {
                     if(num[j] == num[i]){
                         samelist[samelist.Count-1].Add(num[i]);
                         ++i;
                         ++j;
                     }
                     else break;
                 }
              }
            }
            for(int i = 0; i < samelist.Count; ++i){
                Console.WriteLine("Same List index " + i);
                Console.WriteLine("Value = " + samelist[i][0]);
                Console.WriteLine("Count = " + samelist[i].Count);
            }

(written and moved from rextester - spacing’s a little whacky). :slight_smile:

I was sure it could be done with linq somehow and it seems so: c# - Linq query to get the distinct values in a list - Stack Overflow

Am I missing something here? What is wrong with just stopping one element before the end of the list?

private void MoveInPath()
    {
        List<float> samedistances = new List<float>();

        for (int i = 0; i < pairList.Count - 1; i++)
        {
            if (pairList[i].Distance == pairList[i+1].Distance)
            {
                samedistances.Add(pairList[i].Distance);
            }
        }
    }

I had originally mentioned that (though didn’t put it in my own example). However, I got the impression his question was asking for a little more than a snippet like that… :slight_smile:

Okay, just reread the question. My solution is only the first step, although it will fix the error.

This is actually a surprisingly common problem I encounter in my day job. The trick is to look for the differences, not the equality. Its basically an edge detection problem.