Solved

Sort ASP\VBScript Array

Posted on 2008-10-23
9
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Time comparison asp 1 22
window close link 7 35
Session on Html 8 42
Making Table Thru ASP Response.write 5 20
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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