2008-10-01 15 views
6

में संग्रह को सॉर्ट करना यह एक साधारण सवाल है - मैं संग्रह कैसे क्रमबद्ध करूं?क्लासिक एएसपी

मुझे यादृच्छिक क्रम में पंक्तियों के साथ एक CSV फ़ाइल मिली है। मैं पंक्तियों को एक कॉलम में दिनांक के अनुसार क्रमबद्ध करना चाहता हूं। क्या मैं पंक्तियों को एक रिकॉर्डसेट में जोड़ता हूं? क्या मैं एक Scripting.Dictionary के साथ सॉर्ट कर सकता हूँ?

मुझे स्पष्ट रूप से .NET और Linq के साथ खराब कर दिया गया है, और अब मैं खुद को क्लासिक एएसपी की भूमि में वापस पाता हूं, यह महसूस करता हूं कि मुझे यह 7 साल पहले जाना चाहिए था, और जेनेरिकों को बहुत याद आ रही थी। मैं एक पूर्ण n00b की तरह महसूस करता हूँ।

उत्तर

14

इस मामले में मुझे बड़े भाई नेट से मदद मिलेगी। सिस्टम का उपयोग करना संभव है। चयन। वर्गीकृत सूची अपने एएसपी ऐप के भीतर और अपने मुख्य मूल्य जोड़े को क्रमबद्ध करें।

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.IO.StringWriter
  • System.IO.MemoryStream;

यह भी देखें: Marvels of COM .NET interop

+0

प्रतिभा! एक इलाज – harriyott

+0

एफवाईआई का काम करता है, सूची स्वचालित रूप से कुंजी द्वारा क्रमबद्ध की जाती है और सूची द्वारा मूल्यों को क्रमबद्ध करना संभव नहीं है। –

0

यह मेरे लिए भी एक लंबा समय रहा है। आईआईआरसी आपके पास बॉक्स से बाहर विकल्प नहीं है।

यदि मैं आप थे तो मैं सभी डेटा को एक सरणी में डाल दूंगा और फिर सरणी को सॉर्ट करूंगा। मुझे यहां एक क्विकसार्ट कार्यान्वयन मिला: http://www.4guysfromrolla.com/webtech/012799-3.shtml

+0

पूरी तरह से Offtopic, लेकिन मैं अपने लाइवजर्नल में एक Userpic के रूप में है कि एक ही आइकन था। :) – Wayne

+0

मुझे इसे इंटरनेट से कहीं दूर मिला, इसलिए मैंने सोचा कि वास्तव में एक उच्च संभावना है कि कोई और इसका भी उपयोग कर रहा है :) – rslite

3

मैं RecordSet दृष्टिकोण के साथ जाना चाहते हैं। पाठ चालक का प्रयोग करें। आपको कनेक्शन स्ट्रिंग और चयनित कथन में फ़ाइल नाम में निर्देशिका को बदलने की आवश्यकता होगी। विस्तारित संपत्ति "एचडीआर = हां" निर्दिष्ट करती है कि सीएसवी में एक हेडर पंक्ति है जो मैं सुझाव देता हूं क्योंकि यह 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 

%> 
+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') का उपयोग कर सकते हैं। –

संबंधित मुद्दे