2008-10-01 12 views
6

꽤 간단한 질문입니다. 컬렉션을 어떻게 정렬합니까?클래식 ASP에서 컬렉션 정렬

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

나는 분명히 .NET과 Linq로 버릇이 있었고, 이제는 고전적인 ASP의 땅으로 돌아 왔고, 7 년 전에이 사실을 알고 있었음에 틀림 없다는 것을 깨달았고, 제네릭을 놓치고 있습니다. 나는 완전한 n00b 인 것 같은 기분이 든다.

답변

14

이 경우에는 큰 형님 .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.Hashtable
  • System.IO.StringWriter
  • System.IO.MemoryStream;

또한 참조 : 이것에 Marvels of COM .NET interop

+0

천재! 치료 – harriyott

+0

FYI를 사용하면 목록이 자동으로 키순으로 정렬되므로 목록별로 값을 정렬 할 수 없습니다. –

0

내게 너무 오랜 시간이되었습니다. IIRC에는 구입 즉시 사용할 수있는 옵션이 없습니다.

나는 모든 데이터를 배열에 넣은 다음 배열을 정렬합니다. 여기에 QuickSort 구현이 있습니다. http://www.4guysfromrolla.com/webtech/012799-3.shtml

+0

가 완전히 논외,하지만 난 내 라이브 저널에서 userpic으로 같은 아이콘을했다. :) – Wayne

+0

나는 인터넷에서 어딘가에 그것을 가지고있어, 그래서 나는 누군가가 너무 그것을 사용하고있을 가능성이 매우 높다고 생각했다 :) – rslite

3

나는 레코드 접근 방식으로 갈 것. 텍스트 드라이버를 사용하십시오. 연결 문자열의 디렉토리와 select 문에서 파일 이름을 변경해야합니다. 확장 속성 "HDR = Yes"는 CSV에 머리말 행이 있음을 지정합니다.이 행은 가짜 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 

%> 
+0

나는 그 생각을해야만했다.감사합니다 –

0

늦게 늦게 대답을하지만, 여전히 가치.

소규모 컬렉션으로 작업 할 때마다 각 항목마다 정확한 위치에 항목을 삽입하여 각 추가시 효과적으로 컬렉션을 재구성 할 수있었습니다. 다음과 같이

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 
+0

죄송합니다. 구문 강조 표시시 코드가 약간 어색해 보입니다. –

+1

[언어 힌트] (http://meta.stackexchange.com/questions/981/syntax-highlighting-language-hints) (특히'lang-vb')를 사용하여 강조 표시가 올바르게 작동하도록 할 수 있습니다. –

관련 문제