8

これは非常に単純な質問です。コレクションを並べ替えるにはどうすればよいですか?

ランダムな順序で行を含む CSV ファイルがあります。1列の日付に従って行を並べ替えたいと思います。行をレコードセットに追加しますか? Scripting.Dictionary で並べ替えることができますか?

私は明らかに.NETとLinqに甘やかされており、今では古典的なASPの世界に戻っていることに気づき、7年前にこれを知っていたに違いないことに気づき、ジェネリックが大幅に不足しています。私は完全なn00bのように感じます.

4

5 に答える 5

17

この場合、兄貴の .net から助けを得るでしょう。ASP アプリ内でSystem.Collections.Sortedlistを使用して、キーと値のペアを並べ替えることができます。

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

ところで、次の .net クラスも利用できる場合:

  • System.Collections.Queue
  • System.Collections.Stack
  • System.Collections.ArrayList
  • System.Collections.SortedList
  • System.Collections.Hashtable
  • System.IO.StringWriter
  • System.IO.MemoryStream;

参照: COM .NET 相互運用の驚異

于 2008-10-01T07:50:42.957 に答える
3

私は RecordSet アプローチを採用します。テキストドライバーを使用します。接続文字列のディレクトリと 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

%>
于 2008-10-01T06:51:17.313 に答える
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
于 2012-05-31T14:20:21.630 に答える
0

私もお久しぶりです。IIRCには、すぐに使用できるオプションはありません。

私があなたなら、すべてのデータを配列に入れてから、配列を並べ替えます。ここで QuickSort の実装を見つけました: http://www.4guysfromrolla.com/webtech/012799-3.shtml

于 2008-10-01T06:41:49.707 に答える
0

「Bubble Sort」も見てください。これらの従来の ASP タグ クラウドとうまく連携します。

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

于 2008-10-01T06:47:52.487 に答える