zlg258369 发表于 2010-9-22 15:56:00

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

正在做批打,需要双列条件排序,在网上没找到,自己写了一个。有需要的自己看吧Function Sort(ByVal Data(,) As Double, ByVal List1 As Integer, ByVal Way1 As Integer, ByVal List2 As Integer, ByVal Way2 As Integer) As Object

'list1为主排序列,way1为主列排序方式(0为升序,非0为降序);list2为次排序列,way2为次列排序方式(0为升序,非0为降序)

Dim i As Integer
Dim j As Integer

Dim temp As Double

Dim ind As Integer

Dim index() As Integer

Dim zuobiao() As Double
Dim list(,) As Double

ReDim index(UBound(Data, 1))

ReDim zuobiao(UBound(Data, 1))

ReDim list(UBound(Data, 1), UBound(Data, 2))

MsgBox(UBound(Data, 1))

For i = 0 To UBound(Data, 1) - 1

index(i) = i

zuobiao(i) = Data(i, List1 - 1)

Next

'主列排序
i = 0

Do While i < UBound(Data, 1) - 1

For j = i + 1 To UBound(Data, 1) - 1

If Way1 = 0 Then

If zuobiao(i) > zuobiao(j) Then

temp = zuobiao(j)
zuobiao(j) = zuobiao(i)
zuobiao(i) = temp

ind = index(j)
index(j) = index(i)
index(i) = ind

End If

Else

If zuobiao(i) < zuobiao(j) Then

temp = zuobiao(j)
zuobiao(j) = zuobiao(i)
zuobiao(i) = temp

ind = index(j)
index(j) = index(i)
index(i) = ind

End If

End If

Next

i += 1

Loop

'次列排序
For i = 0 To UBound(Data, 1) - 1

list(i, 0) = Data(index(i), 0)
list(i, 1) = Data(index(i), 1)
list(i, 2) = Data(index(i), 2)
list(i, 3) = Data(index(i), 3)

index(i) = i

zuobiao(i) = list(i, List2 - 1)

Next

i = 0

Dim kg As Boolean = True

Do While i < UBound(Data, 1) - 1

If list(i, List1 - 1) = list(i + 1, List1 - 1) And kg = True Then

j = i
kg = False

ElseIf list(i, List1 - 1) <> list(i + 1, List1 - 1) And kg = False Then

Do While j < i

For n As Integer = j + 1 To i

If Way2 = 0 Then

If zuobiao(j) > zuobiao(n) Then

temp = zuobiao(j)
zuobiao(j) = zuobiao(n)
zuobiao(n) = temp

ind = index(j)
index(j) = index(n)
index(n) = ind

End If

Else

If zuobiao(j) < zuobiao(n) Then

temp = zuobiao(j)
zuobiao(j) = zuobiao(n)
zuobiao(n) = temp

ind = index(j)
index(j) = index(n)
index(n) = ind

End If

End If

Next
j += 1
Loop

kg = True

End If

i += 1

Loop

For i = 0 To UBound(Data, 1) - 1

Data(i, 0) = list(index(i), 0)
Data(i, 1) = list(index(i), 1)
Data(i, 2) = list(index(i), 2)
Data(i, 3) = list(index(i), 3)

Next

Sort = Data

End Function

雪山飞狐_lzh 发表于 2010-9-22 22:11:00

本帖最后由 作者 于 2010-9-22 22:34:19 编辑

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

zlg258369 发表于 2010-9-25 12:10:00

<p>学习一下</p>

mkhsj929 发表于 2010-10-27 12:12:00

<p>用LINQ很简单</p>
<p>例:</p>
<p><font face="Verdana">List&lt;Point3d&gt; Pts=new List&lt;Point3d&gt;();<br/>var ptstmp = from pt in Pts orderby pt.X, pt.Y select pt; //按x为主,y为辅的升序排列</font></p>
<p><font face="Verdana">Pts = ptstmp.ToList&lt;Point3d&gt;();</font></p>

羊羊羊 发表于 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,急用多谢!!!
页: [1]
查看完整版本: [原创]数组按双列条件排序