Solved

Sort ASP\VBScript Array

Posted on 2008-10-23
9
511 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

12 Experts available now in Live!

Get 1:1 Help Now