[匯入]ASP常用函數:SortArray2()

來源:互聯網
上載者:User

<%
'功能:對一個二維數組的指定列進行排序,DESC為倒序
'來源:http://jorkin.reallydo.com/article.asp?id=447
'需要Swap過程:http://jorkin.reallydo.com/article.asp?id=446

Function SortArray2(ByVal aSortArray(), ByVal iSortColumn, ByVal sSortDirection)
    On Error Resume Next
    Dim i, j, k
    Dim sValue, JValue
    Dim iLBound, iUBound
    Dim tmp
    Dim iVarType, iSortDirection, iCheckIndex
    iLBound = LBound(aSortArray, 2)
    iUBound = UBound(aSortArray, 2)
    Select Case UCase(sSortDirection)
        Case "DESC", "D"
            iSortDirection = 1
        Case Else
            iSortDirection = -1
    End Select
    iSortColumn = Bint(iSortColumn)
    If iSortColumn < 0 Or iSortColumn > UBound(aSortArray, 1) Then
        SortArray2 = aSortArray
        Exit Function
    End If
    iCheckIndex = iLBound
    While Len(Trim(aSortArray(iSortColumn, iCheckIndex))) = 0 And iCheckIndex < UBound(aSortArray, 2)
        iCheckIndex = iCheckIndex + 1
    Wend
    If IsDate(Trim(aSortArray(iSortColumn, iCheckIndex))) Then
        iVarType = 7
    Else
        If IsNumeric(Trim(aSortArray(iSortColumn, iCheckIndex))) Then
            iVarType = 5
        Else
            iVarType = 8
        End If
    End If
    For i = iLBound To iUBound - 1
        sValue = aSortArray(iSortColumn, i)
        JValue = i
        For j = i + 1 To iUBound
            Select Case iVarType
                Case 8
                    If StrComp(aSortArray(iSortColumn, j), sValue, 1) = iSortDirection Then
                        sValue = aSortArray(iSortColumn, j)
                        JValue = j
                    End If
                Case 7
                    If iSortDirection = -1 Then
                        If DateDiff("s", aSortArray(iSortColumn, j), sValue) > 0 Then
                            sValue = aSortArray(iSortColumn, j)
                            JValue = j
                        End If
                    Else
                        If DateDiff("s", aSortArray(iSortColumn, j), sValue) < 0 Then
                            sValue = aSortArray(iSortColumn, j)
                            JValue = j
                        End If
                    End If
                Case 5
                    If iSortDirection = -1 Then
                        If CDbl(aSortArray(iSortColumn, j)) < CDbl(sValue) Then
                            sValue = aSortArray(iSortColumn, j)
                            JValue = j
                        End If
                    Else
                        If CDbl(aSortArray(iSortColumn, j)) > CDbl(sValue) Then
                            sValue = aSortArray(iSortColumn, j)
                            JValue = j
                        End If

                    End If
            End Select
        Next
        If JValue <> i Then
            For k = 0 To UBound(aSortArray, 1)
                Swap aSortArray(k, JValue) , aSortArray(k, i)
            Next
        End If
    Next
    SortArray2 = aSortArray
End Function
%>

文章來源:http://Jorkin.Reallydo.Com/default.asp?id=447

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.