Solved

Sort ASP\VBScript Array

Posted on 2008-10-23
9
517 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
Technology Partners: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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