views:

1859

answers:

4

It's quite a simple question - how do I sort a collection?

I've got a CSV file with rows in a random order. I'd like to sort the rows according to the date in one column. Do I add the rows to a recordset? Can I sort with a Scripting.Dictionary?

I've clearly been spoilt with .NET and Linq, and now I find myself back in the land of classic asp, realising I must have known this 7 years ago, and missing generics immensely. I feel like a complete n00b.

A: 

It's been a long time for me too. IIRC you don't have an option out of the box.

If I were you I'd put all the data in an array and then sort the array. I found a QuickSort implementation here: http://www.4guysfromrolla.com/webtech/012799-3.shtml

rslite
Completely offtopic, but I had that same icon as a userpic in my livejournal. :)
Wayne
I got it somewhere off the internet, so I thought there is a really high possibility that somebody else might be using it too :)
rslite
A: 

Also look at the "Bubble Sort", works excellent with those classic asp tag cloud.

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

Saif Khan
+3  A: 

I'd go with the RecordSet approach. Use the Text Driver. You'll need to change the directory in the connection string and the filename in the select statement. the Extended Property "HDR=Yes" specifies that there's a header row in the CSV which I suggest as it will make writing the psuedo SQL easier.

<%

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

%>
Wayne
I should've thought of that...a play on memory. Thanks
Saif Khan
+6  A: 

In this case I would get help from big brother .net. It's possible to use System.Collections.Sortedlist within your ASP app and get your key value pairs sorted.

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 if the following .net classes are available too:

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

Also see: Marvels of COM .NET interop

Michal
Genius! Works a treat
harriyott