C# 对向量列表进行排序的最简单方法3';s

C# 对向量列表进行排序的最简单方法3';s,c#,sorting,unity3d,C#,Sorting,Unity3d,所以我有一个独特的问题。假设我有以下代码: List<Vector3> locations = new List<Vector3> (); locations.add(new Vector3(1,1,0)); locations.add(new Vector3(1,2,0)); locations.add(new Vector3(1,3,0)); locations.add(new Vector3(2,1,0)); locations.add(new Vector3(3

所以我有一个独特的问题。假设我有以下代码:

List<Vector3> locations = new List<Vector3> ();

locations.add(new Vector3(1,1,0));
locations.add(new Vector3(1,2,0));
locations.add(new Vector3(1,3,0));
locations.add(new Vector3(2,1,0));
locations.add(new Vector3(3,2,0));
locations.add(new Vector3(3,8,0));

谢谢你的帮助

以下是我为获得所需的正确排序路径所做的操作。如果有更有效的方法,请告诉我

List<int> index = new List<int> ();

index.Clear ();

foreach (Vector3 location in locations) {
    foreach (Vector3 comparedLocation in locations) {
        if (location == comparedLocation) {
            continue;
        }
        if ((location.x == comparedLocation.x) && (location.y > comparedLocation.y)) {
            int id = missingLocations.IndexOf (location);
            index.Add (id);
        }
    }
}

index.Sort ();
index = index.Distinct ().ToList ();

for (int i = index.Count - 1; i >= 0; i--) {
    int id = index [i];
    locations.RemoveAt (id);
}
列表索引=新列表();
index.Clear();
foreach(位置中的矢量3位置){
foreach(位置中的矢量3比较位置){
如果(位置==比较位置){
继续;
}
if((location.x==comparedLocation.x)&&(location.y>comparedLocation.y)){
int id=missingLocations.IndexOf(位置);
索引。添加(id);
}
}
}
Sort();
index=index.Distinct().ToList();
对于(inti=index.Count-1;i>=0;i--){
int id=索引[i];
locations.RemoveAt(id);
}
使用
列表
使用
Vector3[]
您只需使用以下命令按x升序对位置
Vector3[]
进行排序:

locations = locations.OrderBy(v => v.x).ToArray<Vector3>();
locations=locations.OrderBy(v=>v.x.ToArray();

你能用一个简单的
.GroupBy(v=>v.x)选择(g=>g.Min())
或者类似的东西吗?我认为您应该扩展删除条件并更新问题标题——这很重要。我将它们存储在一个列表中,因为我不知道通过我已经编写的代码将添加多少变量。如果我知道其中有多少矢量3,那么一个数组就可以正常工作。列表让我有更多的自由。请参阅我的更新答案,了解如何使用列表实现
locations = locations.OrderBy(v => v.x).ToList();
locations = locations.OrderBy(v => v.x).ToArray<Vector3>();