저는 List<T>
입니다. 정렬하고 싶기 때문에 List<T>.Sort(Comparison<T>)
을 사용했습니다. 내 코드가 예상대로 작동하지 않고 일부 디버깅 후 항목의 순서가 실제로 변경되었지만 정렬되지는 않았다는 것을 발견했습니다.
코드는 여기에 있습니다 :C# List.Sort가 정렬되지 않습니다.
System.Comparison<Intersection> comp=(Intersection one, Intersection other)=>{//Sort sorts from lowest to highest
if(one.index>other.index){
return 1;
}
else if(one.index<other.index){
return -1;
}
else if((one.point-one.node.position).sqrMagnitude>(other.point-other.node.position).sqrMagnitude){
return 1;
}
else{
return -1;
}
};
intersections.Sort(comp);
문제는 정렬 후 항목이 네 번째 내가 뭔가 문제가있을 수 있습니다 생각 6.있는 동안 세 번째 항목은 인덱스 7을 갖도록하기 위해에서 발견 될 수있다 비교 람다,하지만 동일한 함수를 사용하여 순차적 인 항목을 비교하는 코드를 추가했지만 올바르게 동작하고 때로는 1을 반환하므로 문제가 분명히 다른 곳에서 발생합니다.
는 이후에 비교 :
for(int he=1; he<intersections.Count; he++){
Debug.Log(comp(intersections[he-1], intersections[he]));
}
내가 부족 또는 내 List<T>.Sort
구현에 오류가 난 그냥 내 자신의 정렬 방법을해야 뭔가가 있나요? 다른 사람도 언급 한 바와 같이
class Intersection{
public PolyNode node;
public int index;
public Polygon poly;
public Intersection sister;
public bool is_out;
public sbyte wallnum;
public Vector2 point;
public int list_index;
}
,하지만 그들은 결코 : 더 긴 토론을위한
, 나는 잘못된 비교 알고리즘을 작성 본 적이 일반적인 방법에 나의 네 부분으로 시리즈를 참조 등가 (0)? – CodeCaster
동일한 가치는 무엇입니까? 결코 0을 반환하지 않는 비교는 나를 나쁘게 보입니다. 문제를 보여주는 짧지만 완전한 * 프로그램을 제공해 주시겠습니까? (만약 당신이 단지 하나의 속성을 비교함으로써 그것을 할 수 있다면, 그것은 정말로 도움이 될 것입니다 ...) –
당신이 분류하려고하는 데이터 구조를 보여줍니다. – MethodMan