문제

아주 간단한 질문입니다. 컬렉션을 어떻게 정렬합니까?

무작위 순서로 행이있는 CSV 파일이 있습니다. 한 열에 날짜에 따라 행을 정렬하고 싶습니다. 레코드 세트에 행을 추가합니까? Scripting.dictionary로 정렬 할 수 있습니까?

나는 .NET과 LINQ로 분명히 망쳤다. 그리고 이제 나는 고전적인 ASP의 땅에 다시 돌아왔다. 나는 7 년 전에이 사실을 알고 있어야한다는 것을 깨달았고 제네릭이 엄청나게 빠졌다는 것을 깨달았다. 나는 완전한 N00B처럼 느낍니다.

도움이 되었습니까?

해결책

이 경우 Big Brother .NET로부터 도움을받을 것입니다. 사용할 수 있습니다 System.collections.sortedlist ASP 앱 내에서 키 값 쌍을 정렬하십시오.

set list = server.createObject("System.Collections.Sortedlist")
with list
  .add "something", "YY"
  .add "something else", "XX"
end with

for i = 0 to list.count - 1
    response.write(list.getKey(i) & " = " & list.getByIndex(i))
next

BTW 다음 .NET 클래스도 사용할 수있는 경우 :

  • System.collections.queue
  • System.Collections.Stack
  • System.Collections.ArrayList
  • System.collections.sortedlist
  • System.collections.hashtable
  • System.io.StringWriter
  • System.io.memorystream;

또한 참조 : com .net interop의 경이로움

다른 팁

나는 레코드 세트 접근법과 함께 갈 것이다. 텍스트 드라이버를 사용하십시오. Connection String의 디렉토리와 Select 문의 파일 이름을 변경해야합니다. 확장 된 속성 "HDR = 예"는 CSV에 헤더 행이 있음을 지정하여 PSUEDO SQL을 더 쉽게 작성할 수 있도록 제안합니다.

<%

Dim strConnection, conn, rs, strSQL

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\inetpub\wwwroot\;Extended Properties='text;HDR=Yes;FMT=Delimited';"

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open strConnection

Set rs = Server.CreateObject("ADODB.recordset")
strSQL = "SELECT * FROM test.csv order by date desc"
rs.open strSQL, conn, 3,3

WHILE NOT rs.EOF
    Response.Write(rs("date") & "<br/>") 
    rs.MoveNext
WEND

rs.Close
Set rs = Nothing

conn.Close
Set conn = Nothing

%>

나에게도 오랜 시간이 걸렸습니다. IIRC 당신은 상자에 옵션이 없습니다.

내가 당신이라면 모든 데이터를 배열에 넣고 배열을 정렬 할 것입니다. 여기에서 QuickSort 구현을 찾았습니다. http://www.4guysfromrolla.com/webtech/012799-3.shtml

또한 "Bubble Sort"를 살펴보면 클래식 ASP 태그 클라우드에서 우수하게 작동합니다.

http://www.4guysfromrolla.com/webtech/011001-1.shtml

이것에 대한 늦은 늦은 답변이지만 여전히 가치가 있습니다.

나는 작은 컬렉션으로 작업하고 있었기 때문에 매번 올바른 장소에 아이템을 삽입 한 접근 방식을 감당할 수 있었으며, 각 추가에 대한 컬렉션을 효과적으로 재구성했습니다.

vbscript 클래스는 다음과 같습니다.

'Simple collection manager class.
'Performs the opration of adding/setting a collection item.
'Encapulated off here in order to delegate responsibility away from the collection class.
Class clsCollectionManager
    Public Sub PopulateCollectionItem(collection, strKey, Value)
        If collection.Exists(strKey) Then
            If (VarType(Value) = vbObject) Then
                Set collection.Item(strKey) = Value
            Else
                collection.Item(strKey) = Value
            End If
        Else
            Call collection.Add(strKey, Value)
        End If
    End Sub

    'take a collection and a new element as input parameters, an spit out a brand new collection 
    'with the new item iserted into the correct location by order
    'This works on the assumption that the collection it is receiving is already ordered 
    '(which it should be if we always use this method to populate the item)

    'This mutates the passed collection, so we highlight this by marking it as byref 
    '(this is not strictly necessary as objects are passed by reference anyway)
    Public Sub AddCollectionItemInOrder(byref existingCollection, strNewKey, Value)
        Dim orderedCollection: Set orderedCollection = Server.CreateObject("Scripting.Dictionary")
        Dim strExistingKey

        'If there is something already in our recordset then we need to add it in order.

        'There is no sorting available for a collection (or an array) in VBScript. Therefore we have to do it ourself.
        'First, iterate over eveything in our current collection. We have to assume that it is itself sorted.
        For Each strExistingKey In existingCollection

            'if the new item doesn't exist AND it occurs after the current item, then add the new item in now 
            '(before adding in the current item.)
            If (Not orderedCollection.Exists(strNewKey)) And (strExistingKey > strNewKey) Then
                Call PopulateCollectionItem(orderedCollection, strNewKey, Value)
            End If
            Call PopulateCollectionItem(orderedCollection, strExistingKey, existingCollection.item(strExistingKey))
        Next

        'Finally check to see if it still doesn't exist. 
        'It won't if the last place for it is at the very end, or the original collection was empty
        If (Not orderedCollection.Exists(strNewKey)) Then
            Call PopulateCollectionItem(orderedCollection, strNewKey, Value)
        End If

        Set existingCollection = orderedCollection
    End Sub
End Class
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top