这是一个非常简单的问题——如何对集合进行排序?
我有一个随机顺序行的 CSV 文件。我想根据一列中的日期对行进行排序。我是否将行添加到记录集中?我可以使用 Scripting.Dictionary 进行排序吗?
我显然已经被 .NET 和 Linq 宠坏了,现在我发现自己又回到了经典的 asp 领域,意识到我必须在 7 年前就知道这一点,并且非常缺少泛型。我觉得自己像一个完整的n00b。
这是一个非常简单的问题——如何对集合进行排序?
我有一个随机顺序行的 CSV 文件。我想根据一列中的日期对行进行排序。我是否将行添加到记录集中?我可以使用 Scripting.Dictionary 进行排序吗?
我显然已经被 .NET 和 Linq 宠坏了,现在我发现自己又回到了经典的 asp 领域,意识到我必须在 7 年前就知道这一点,并且非常缺少泛型。我觉得自己像一个完整的n00b。
在这种情况下,我会从大哥.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 类也可用:
另请参阅:COM .NET 互操作的奇迹
我会使用 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
%>
对我来说也已经很久了。IIRC您没有开箱即用的选项。
如果我是你,我会将所有数据放入一个数组中,然后对数组进行排序。我在这里找到了一个快速排序实现:http ://www.4guysfromrolla.com/webtech/012799-3.shtml
还请查看“冒泡排序”,它与那些经典的 asp 标签云效果很好。
一个迟到的答案,但仍然有价值。
我正在处理小型收藏品,因此可以负担得起每次将项目插入正确位置的方法,从而在每次添加时有效地重建收藏品。
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