明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1840|回复: 4

[原创]数组按双列条件排序

[复制链接]
发表于 2010-9-22 15:56:00 | 显示全部楼层 |阅读模式
正在做批打,需要双列条件排序,在网上没找到,自己写了一个。有需要的自己看吧
  1. Function Sort(ByVal Data(,) As Double, ByVal List1 As Integer, ByVal Way1 As Integer, ByVal List2 As Integer, ByVal Way2 As Integer) As Object
  2. 'list1为主排序列,way1为主列排序方式(0为升序,非0为降序);list2为次排序列,way2为次列排序方式(0为升序,非0为降序)
  3. Dim i As Integer
  4. Dim j As Integer
  5. Dim temp As Double
  6. Dim ind As Integer
  7. Dim index() As Integer
  8. Dim zuobiao() As Double
  9. Dim list(,) As Double
  10. ReDim index(UBound(Data, 1))
  11. ReDim zuobiao(UBound(Data, 1))
  12. ReDim list(UBound(Data, 1), UBound(Data, 2))
  13. MsgBox(UBound(Data, 1))
  14. For i = 0 To UBound(Data, 1) - 1
  15. index(i) = i
  16. zuobiao(i) = Data(i, List1 - 1)
  17. Next
  18. '主列排序
  19. i = 0
  20. Do While i < UBound(Data, 1) - 1
  21. For j = i + 1 To UBound(Data, 1) - 1
  22. If Way1 = 0 Then
  23. If zuobiao(i) > zuobiao(j) Then
  24. temp = zuobiao(j)
  25. zuobiao(j) = zuobiao(i)
  26. zuobiao(i) = temp
  27. ind = index(j)
  28. index(j) = index(i)
  29. index(i) = ind
  30. End If
  31. Else
  32. If zuobiao(i) < zuobiao(j) Then
  33. temp = zuobiao(j)
  34. zuobiao(j) = zuobiao(i)
  35. zuobiao(i) = temp
  36. ind = index(j)
  37. index(j) = index(i)
  38. index(i) = ind
  39. End If
  40. End If
  41. Next
  42. i += 1
  43. Loop
  44. '次列排序
  45. For i = 0 To UBound(Data, 1) - 1
  46. list(i, 0) = Data(index(i), 0)
  47. list(i, 1) = Data(index(i), 1)
  48. list(i, 2) = Data(index(i), 2)
  49. list(i, 3) = Data(index(i), 3)
  50. index(i) = i
  51. zuobiao(i) = list(i, List2 - 1)
  52. Next
  53. i = 0
  54. Dim kg As Boolean = True
  55. Do While i < UBound(Data, 1) - 1
  56. If list(i, List1 - 1) = list(i + 1, List1 - 1) And kg = True Then
  57. j = i
  58. kg = False
  59. ElseIf list(i, List1 - 1) <> list(i + 1, List1 - 1) And kg = False Then
  60. Do While j < i
  61. For n As Integer = j + 1 To i
  62. If Way2 = 0 Then
  63. If zuobiao(j) > zuobiao(n) Then
  64. temp = zuobiao(j)
  65. zuobiao(j) = zuobiao(n)
  66. zuobiao(n) = temp
  67. ind = index(j)
  68. index(j) = index(n)
  69. index(n) = ind
  70. End If
  71. Else
  72. If zuobiao(j) < zuobiao(n) Then
  73. temp = zuobiao(j)
  74. zuobiao(j) = zuobiao(n)
  75. zuobiao(n) = temp
  76. ind = index(j)
  77. index(j) = index(n)
  78. index(n) = ind
  79. End If
  80. End If
  81. Next
  82. j += 1
  83. Loop
  84. kg = True
  85. End If
  86. i += 1
  87. Loop
  88. For i = 0 To UBound(Data, 1) - 1
  89. Data(i, 0) = list(index(i), 0)
  90. Data(i, 1) = list(index(i), 1)
  91. Data(i, 2) = list(index(i), 2)
  92. Data(i, 3) = list(index(i), 3)
  93. Next
  94. Sort = Data
  95. End Function

评分

参与人数 1明经币 +1 收起 理由
雪山飞狐_lzh + 1 【好评】表扬一下

查看全部评分

发表于 2010-9-22 22:11:00 | 显示全部楼层
本帖最后由 作者 于 2010-9-22 22:34:19 编辑

双列条件排序?
可以考虑用数组的数组,然后对其中的某一项排序
简单的例子:
  1.         Dim arr As Double()() = New Double()() {New Double() {5, 3, 4}, New Double() {3, 2, 1}}
  2.         Array.Sort(arr(0))
  3.         Array.Sort(arr(1))
  4.         Array.Reverse(arr(0))
另外way1/2应考虑使用boolean型

 楼主| 发表于 2010-9-25 12:10:00 | 显示全部楼层

学习一下

发表于 2010-10-27 12:12:00 | 显示全部楼层

用LINQ很简单

例:

List<oint3d> Pts=new List<oint3d>();
var ptstmp = from pt in Pts orderby pt.X, pt.Y select pt; //按x为主,y为辅的升序排列

Pts = ptstmp.ToList<oint3d>();

评分

参与人数 1明经币 +1 收起 理由
雪山飞狐_lzh + 1 【好评】好思路

查看全部评分

发表于 2012-8-23 15:48:10 | 显示全部楼层
用LINQ很简单

例:

List<Point3d> Pts=new List<Point3d>();
var ptstmp = from pt in Pts orderby pt.X, pt.Y select pt; //按x为主,y为辅的升序排列

Pts = ptstmp.ToList<Point3d>();

这段代码能否转化为vb.net,急用多谢!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:50 , Processed in 0.173776 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表