Solved

Sort ASP\VBScript Array

Posted on 2008-10-23
9
513 Views
Last Modified: 2010-04-21
Hello,

In a Classic ASP application I have a string array (aTable1) where the contents of an element looks like this:

"10001,Smith,John"

Yep, it is really a comma delimited record.  The first field is fixed in width (5 characters).  I want to sort the array on the second field.


What is the easiest way to do this?


Thanks,

Michael
0
Comment
Question by:michael4606
9 Comments
 
LVL 16

Expert Comment

by:brad2575
ID: 22785145
easiest/most effeciant way would be to insert the data into a SQL table (can use a temp table) and the sort it from the SQL table.
0
 

Author Comment

by:michael4606
ID: 22785213
Cool suggestion.  

My error.  I should have qualified it further that I cannot use a SQL database to achieve this.

Thanks for the response,

Michael
0
 
LVL 15

Expert Comment

by:dosth
ID: 22785361
you can build a record set on the fly and append the data to the record set and sort.
0
 
LVL 7

Expert Comment

by:Norush
ID: 22785450
Is your array 1-dimensional ?
Like: "10001,Smith,John"

or 2-dimensional ?
like
"10001,Smith,John"
"10002,Gibson,Paul"
"10003,Jackson,Micheal"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:michael4606
ID: 22785561
It is a 1-dimensional array.  The value of each element is a string that looks like this:

"10000,aaaaa,aaaaa"

So three elements might look like this:

aTable(0), aTable(1) and aTable(2)

"10000,aaaaa,aaaaa"
"10001,bbbbbbb,bbb"
"10002,ccccc,ccccccccc"

The value of the string just happens to be a comma delimited list where the first item is fixed width and the rest are variable width.
0
 
LVL 7

Expert Comment

by:Norush
ID: 22787056
So if you had an element like this:

"10000,b,z,a"
you want to transfer it into  ?
"10000,a,b,z"

and the same for all elements ?
0
 

Author Comment

by:michael4606
ID: 22788013
Norush,

Great question.  Sometimes I am so stupid when posting questions.  Sorry for the lack of clarity.

The answer is no.  I do NOT want to sort the string list within in each array element.  I want to sort all the elements in the array based on the values of the second "field" in the list of each element.  The first "field" in the list always being a fixed value of 5 characters (10001).  So the first positional "field" that I want to sort on would be position 7 (1st field plus comma separator).

Example.  Say I have these array values:

"10002,ccccc,zzz"
"10000,aaaaa,zzz"
"10001,bbbbbbb,zzz"

I want them to be sorted as this:

"10000,aaaaa,zzz"
"10001,bbbbbbb,zzz"
"10002,ccccc,zzz"

Again, sorry for not stating my goal more clearly.

Sincere thanks for your help,

Michael
0
 
LVL 7

Accepted Solution

by:
Norush earned 125 total points
ID: 22788395
Like this?
<%

	Dim arrShort(2)

	Dim arrTemp

	Dim strSecondColumnValue

	Dim strNextSecondColumnValue

	

	arrShort(0) = "10002,ccccc,zzz"

	arrShort(1) = "10000,aaaaa,zzz"

	arrShort(2) = "10001,bbbbbbb,zzz"

	

	for i = UBound(arrShort) - 1 To 0 Step -1

	    for j= 0 to i

			arrTemp = Split(arrShort(j),",")

			strSecondColumnValue = arrTemp(1)

			

			arrTemp = Split(arrShort(j+1),",")

			strNextSecondColumnValue = arrTemp(1)

			

	        if strSecondColumnValue>strNextSecondColumnValue then

	            temp=arrShort(j+1)

	            arrShort(j+1)=arrShort(j)

	            arrShort(j)=temp

	        end if

	    next

	next

	

	Response.Write arrShort(0) & "<BR>"

	Response.Write arrShort(1) & "<BR>"

	Response.Write arrShort(2) & "<BR>"

%>

Open in new window

0
 

Author Closing Comment

by:michael4606
ID: 31509156
Norush,

Thanks that is perfect!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now