Solved

Sort ASP\VBScript Array

Posted on 2008-10-23
9
514 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

803 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