2017-12-01 9 views
0

나는 X와 Y 값으로 객체의 데이터 테이블을 정렬하려고하므로 Y로 정렬 된 다음 X로 정렬됩니다. 이제는 정상적으로 작동하지만 이상하게 생각합니다. 허용 오차 값을 추가하여 두 Y 값이 해당 허용 범위 내에있을 때 해당 Y 값이 동일한 것으로 보이고 해당 X 값에 따라 정렬되도록하십시오. 예 :공차로 값 정렬

  • 개체 1은 X=4 and Y=4.1입니다.
  • 오브젝트 2는 X=6 and Y=4입니다.

0.1 차이가 주어진 허용 오차 이내 인 경우이 값을 1로 정렬 한 다음 2로 정렬해야하지만 이제는이 차이로 인해 2와 1로 정렬됩니다. 그것은 Revit API 프로젝트의 일부이지만 일반적인 C# 질문 일뿐입니다. 테이블은 objectList이라는 오브젝트 목록에서 데이터를 가져옵니다. 내가 원하는 것을 성취 할 수있는 방법이 있습니까?

코드 :

// Create table 
DataTable obTable = new DataTable(); 

// id 
DataColumn idColumn = new DataColumn(); 
idColumn.DataType = Type.GetType("System.Int32"); 
idColumn.ColumnName = "id"; 
obTable.Columns.Add(idColumn); 

// X 
DataColumn xColumn = new DataColumn(); 
xColumn.DataType = Type.GetType("System.Int32"); 
xColumn.ColumnName = "X"; 
obTable.Columns.Add(xColumn); 

// Y 
DataColumn yColumn = new DataColumn(); 
yColumn.DataType = Type.GetType("System.Int32"); 
yColumn.ColumnName = "Y"; 
obTable.Columns.Add(yColumn); 

// get data from objectList 
foreach (FamilyInstance ob in objectList) 
{ 
    int id = ob.Id.IntegerValue; 
    LocationPoint L = ob.Location as LocationPoint; 
    int X = (int)L.Point.X; 
    int Y = (int)L.Point.Y; 

    DataRow row; 
    row = obTable.NewRow(); 

    row["id"] = id; 
    row["X"] = X; 
    row["Y"] = Y; 

    obTable.Rows.Add(row); 
} 

// sort table 
DataView dv = new DataView(obTable); 
dv.Sort = "Y ASC, X ASC"; 
DataTable dt = dv.ToTable(); 
+0

@HimBromBeere 고맙습니다 XD –

+0

당신은 통해 Objectlist를 정렬 할 수 있습니다, 당신의 목록을 정렬합니다. 이는 비교 (T) 위임을 구현하여 수행 할 수 있습니다. 자세한 내용은 여기를 참조하십시오. https://msdn.microsoft.com/en-us/library/tfakywbh(v=vs.110).aspx – lamandy

+0

@lamandy하지만 2 개의 값으로 목록을 정렬 할 수 있습니까? Y 값이 같거나 (주어진 허용 오차 내에서) Y이면 Y로 정렬 한 다음 X로 정렬합니다. 그게 왜 내가 정직하게 데이터 테이블을 사용하는 유일한 목적입니까? –

답변

1
private static int CompareWithThreshold(FamilyInstance obj1, FamilyInstance obj2) 
{ 
    LocationPoint point1 = obj1.Location as LocationPoint; 
    LocationPoint point2 = obj2.Location as LocationPoint; 
    double x1 = point1.Point.X; 
    double y1 = point1.Point.Y; 
    double x2 = point2.Point.X; 
    double y2 = point2.Point.Y; 
    double diff = y1 - y2; 
    if (diff >= -0.1 && diff <= 0.1) 
    { 
     return x1.CompareTo(x2); 
    } 
    else 
    { 
     return y1.CompareTo(y2); 
    } 
} 

는 DataTable에 삽입하기 전에 단지

objectList.Sort(CompareWithThreshold); 
+0

이것은 작동합니다! 그냥'compareTo'와'sort'를 대문자로 써야했습니다. 감사! –