一种是用系统自带的Linq语句进行排序,比使用第二种归并排序要快好几倍,推荐使用第一种方法:
    其中GetTextInsertPt(x)获取cad文字对象插入点坐标的方法,在第二种排序方法里面!

    第一种:Linq语句进行排序:速度快!

    1. List<object> objEntArr = new List<object>();
    2. objEntArr = objEntArr.OrderBy(x => MyMethod.GetTextInsertPt(x)[0]).Select(x => x).ToList();

    第二种:归并排序法:速度慢!

    1. /// <summary>
    2. /// 按文字位置坐标排序
    3. /// </summary>
    4. /// <param name="lst">文字集合</param>
    5. /// <param name="axis">对应轴的索引,默认0->X,1->Y,2->Z</param>
    6. /// <param name="isup">升序还是降序排列,默认升序True,降序False</param>
    7. public static void MergeSort_TextPoint(ref List<object> arylst, int axis = 0, bool isup = true)
    8. {
    9. object[] arr = arylst.ToArray();
    10. MergeSort(ref arr, 0, arr.Length - 1, axis, isup);
    11. arylst = arr.ToList();
    12. }
    13. /// <summary>
    14. /// 按文字位置坐标排序
    15. /// </summary>
    16. /// <param name="lst">文字集合</param>
    17. /// <param name="axis">对应轴的索引,默认0->X,1->Y,2->Z</param>
    18. /// <param name="isup">升序还是降序排列,默认升序True,降序False</param>
    19. public static void MergeSort_TextPoint(ref object[] arr, int axis = 0, bool isup = true)
    20. {
    21. MergeSort(ref arr, 0, arr.Length - 1, axis, isup);
    22. }
    23. private static void MergeSort(ref object[] arr, int ArrMin, int ArrMax, int axis, bool isup)
    24. {
    25. if (ArrMax > ArrMin)
    26. {
    27. int ArrMid = (ArrMin + ArrMax) / 2;
    28. MergeSort(ref arr, ArrMin, ArrMid, axis, isup);
    29. MergeSort(ref arr, ArrMid + 1, ArrMax, axis, isup);
    30. Merge(ref arr, ArrMin, ArrMid, ArrMax, axis, isup);
    31. }
    32. }
    33. private static void Merge(ref object[] arr, int ArrMin, int ArrMid, int ArrMax, int axis, bool isup)
    34. {
    35. bool result;
    36. object[] temp = new object[ArrMax - ArrMin + 1];
    37. int i = ArrMin;
    38. int j = ArrMid + 1;
    39. int k = 0;
    40. while (i <= ArrMid && j <= ArrMax)
    41. {
    42. string str1 = GetTextstr((AcadObject)arr[i]);
    43. double[] basept1 = GetTextInsertPt((AcadObject)arr[i]);
    44. string str2 = GetTextstr((AcadObject)arr[j]);
    45. double[] basept2 = GetTextInsertPt((AcadObject)arr[j]);
    46. if (isup)
    47. {
    48. result = basept1[axis] < basept2[axis];
    49. }
    50. else
    51. {
    52. result = basept1[axis] > basept2[axis];
    53. }
    54. if (result)
    55. {
    56. temp[k] = arr[i];
    57. k++;
    58. i++;
    59. }
    60. else
    61. {
    62. temp[k] = arr[j];
    63. k++;
    64. j++;
    65. }
    66. }
    67. while (i <= ArrMid)
    68. {
    69. temp[k] = arr[i];
    70. k++;
    71. i++;
    72. }
    73. while (j <= ArrMax)
    74. {
    75. temp[k] = arr[j];
    76. k++;
    77. j++;
    78. }
    79. for (k = 0, i = ArrMin; i <= ArrMax; k++, i++)
    80. {
    81. arr[i] = temp[k];
    82. }
    83. }
    84. /// <summary>
    85. /// 获取文字插入点坐标
    86. /// </summary>
    87. /// <param name="Obj">*Text object 对象</param>
    88. /// <returns>插入点坐标</returns>
    89. public static double[] GetTextInsertPt(object Obj)
    90. {
    91. if (((AcadObject)Obj).ObjectName.Equals("AcDbText"))
    92. {
    93. return ((AcadText)Obj).InsertionPoint;
    94. }
    95. else
    96. {
    97. return ((AcadMText)Obj).InsertionPoint;
    98. }
    99. }