فرز مجموعة في ASP الكلاسيكي
-
03-07-2019 - |
سؤال
إنه سؤال بسيط للغاية - كيف يمكنني فرز مجموعة؟
لدي ملف CSV يحتوي على صفوف بترتيب عشوائي.أرغب في فرز الصفوف حسب التاريخ في عمود واحد.هل أقوم بإضافة الصفوف إلى مجموعة السجلات؟هل يمكنني الفرز باستخدام قاموس Scripting.Dictionary؟
من الواضح أنني كنت مدللًا بـ .NET وLinq، والآن أجد نفسي مرة أخرى في أرض ASP الكلاسيكية، مدركًا أنني لا بد أن أعرف هذا منذ 7 سنوات، وأفتقد الأدوية الجنيسة بشكل كبير.أشعر وكأنني n00b كاملة.
المحلول
في هذه الحالة سأحصل على مساعدة من الأخ الأكبر .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
راجع للشغل إذا كانت فئات .net التالية متاحة أيضًا:
- System.Collections.Queue
- System.Collections.Stack
- System.Collections.ArrayList
- System.Collections.SortedList
- System.Collections.Hashtable
- System.IO.StringWriter
- System.IO.MemoryStream;
انظر أيضا: عجائب التشغيل المتداخل لـ COM .NET
نصائح أخرى
وكنت اذهب مع النهج السجلات. استخدام برنامج تشغيل نص. سوف تحتاج إلى تغيير الدليل في سلسلة الاتصال واسم الملف في العبارة حدد. والموسع الملكية "HDR = نعم" يحدد أن هناك صف الرأس في 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
وننظر أيضا في "ترتيب فقاعة"، ويعمل ممتاز مع تلك الكلاسيكية العلامة آسيا والمحيط الهادئ سحابة.
والجواب في وقت متأخر إلى ذلك، ولكن لا يزال من القيمة.
وكنت أعمل مع مجموعات صغيرة بحيث يمكن أن تحمل النهج حيث تم إدراج هذا البند في المكان الصحيح في كل مناسبة، وإعادة بناء فعال جمع على كل إضافة.
والطبقة فبسكريبت هي كما يلي:
'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