Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort Multi Array 3

Status
Not open for further replies.

TheCandyman

Technical User
Sep 9, 2002
761
US
I have the standard Multi Array that need to get sorted, now before you google read my post

sample array:
Code:
1     8      Text   ..... to 9 columns
3     12     Text
2     24     Text
4     5      Text
6     1      Text
5     21     Text

All of the arrays samples i can find sort the elements within each row and leave the rows themselves untouched. I need to find one that won't change the order of the row elements, but will only change the order of the rows.

Want it:
Code:
1     8      Text
2     24     Text
3     12     Text
4     5      Text
5     21     Text
6     1      Text

If this were asp.net it would be easier, but it's just classic asp. Anyone have an example they can share?

 
Hey if this array comes out of a recordset object you can use the built-in .Sort property to do it before building the array.
 
You can run your array through something like this. Make sure you include ADOVBS.inc.

Code:
my_sorted_Array = SortTheResourceArray(my_md_array)

FUNCTION SortTheResourceArray(input_array)
	dim rs
	set rs=createobject("adodb.recordset")
	With rs.Fields
		.Append "col_01",adInteger
		.Append "col_02",adInteger
                ...and so on
	End With
	rs.Open
	IF isArray(input_array) THEN
		FOR i = 0 TO UBOUND(input_array,2)
			rs.AddNew 
			rs("col_01") 	= input_array(0,i)
			rs("col_02") 	= input_array(1,i)
                        ...and so on
			rs.Update
		NEXT
	END IF
	rs.Sort = "col_02 ASC"
	IF NOT rs.BOF AND NOT rs.EOF THEN
		SortTheResourceArray = rs.GetRows()
	END IF
	rs.close
	SET rs = Nothing
END FUNCTION
 
No DB with this one, otherwise it would be simple. I store a few values in a cookie, then pull them out and into an array. But I need to sort for displaying. So, any examples without a DB, just have an array with data??
 
This isn't for a DB; it's a standalone recordset. Input an array regardless of it's from a db or not. This is what Sheco is saying too.
 
travisbrown, never used a recordset without using a DB, i started messing with the code you gave. Are you sure this will sort a multi array? It looks like it will take one value and put in the recordset.
 
I tweaked the code, but still can't get it to work. It's not saving any values into the recordset


Output
Code:
Yes : Inside : No

Code:
FUNCTION SortTheResourceArray(input_array)
	%><!--#include file="../reg/Connections/adovbs.inc" --><%	
	
	If isArray(input_array) Then
		response.write "Yes : "
	Else
		response.write "No : "
	End If 
	
    dim rs
    set rs=createobject("adodb.recordset")
    With rs.Fields
        .Append "col_01",adInteger
        .Append "col_02",adInteger	'adLongVarChar
        .Append "col_03",adInteger	'adLongVarChar
    End With
    rs.Open
    IF isArray(input_array) THEN
    	response.write "Inside : "
        FOR i = 0 TO UBOUND(input_array)
            rs.AddNew
            rs("col_01")     = "1"	'input_array(0,i)
            rs("col_02")     = "2"	'input_array(1,i)
            rs("col_03")     = "3"	'input_array(2,i)
            rs.Update
        NEXT
    END IF
	
    rs.Sort = "col_02 ASC"
    IF NOT rs.BOF AND NOT rs.EOF THEN
    	response.write "Inside2 : "
        SortTheResourceArray = rs.GetRows()
    END IF
    rs.close
    SET rs = Nothing
    
    
    If isArray(SortTheResourceArray) Then
		response.write "Yes"
	Else
		response.write "No"
	End If 
				
END FUNCTION
 
It works excellently. I wrote it to specifically sort MD arrays.

Couple things.

FOR i = 0 TO UBOUND(input_array) must be FOR i = 0 TO UBOUND(input_array,2).

The second parameter specifies to loop for the number of rows rather than the number of columns.

Microsoft, in its wisdom, can't decide if it likes zero based arrays or not. have to add a -1 to the loop to stop n extra row from being added at the top.

See the .sort line for ASC or DESC options.

Run the code below to see the result.

Code:
<!--#include file="ADOVBS.inc" -->

<%
DIM my_array(8,3)

my_array(0,0) = 1
my_array(1,0) = 1
my_array(2,0) = 1
my_array(3,0) = 1
my_array(4,0) = 1
my_array(5,0) = 1
my_array(6,0) = 1
my_array(7,0) = 1
my_array(8,0) = 1

my_array(0,1) = 2
my_array(1,1) = 2
my_array(2,1) = 2
my_array(3,1) = 2
my_array(4,1) = 2
my_array(5,1) = 2
my_array(6,1) = 2
my_array(7,1) = 2
my_array(8,1) = 2

my_array(0,2) = 3
my_array(1,2) = 3
my_array(2,2) = 3
my_array(3,2) = 3
my_array(4,2) = 3
my_array(5,2) = 3
my_array(6,2) = 3
my_array(7,2) = 3
my_array(8,2) = 3

sorted_array = SortTheResourceArray(my_array)

IF isArray(sorted_array) THEN
	response.write "<pre>"
	FOR i = 0 TO UBOUND(sorted_array,2)
		response.write sorted_array(0,i) & vbtab
		response.write sorted_array(1,i) & vbtab
		response.write sorted_array(2,i) & vbtab
		response.write sorted_array(3,i) & vbtab
		response.write sorted_array(4,i) & vbtab
		response.write sorted_array(5,i) & vbtab
		response.write sorted_array(6,i) & vbtab
		response.write sorted_array(7,i) & vbtab
		response.write sorted_array(8,i) & vbtab
		response.write vbcr
	NEXT
	response.write "</pre>"
END IF

FUNCTION SortTheResourceArray(input_array)   
    dim rs
    set rs=createobject("adodb.recordset")
    With rs.Fields
        .Append "col_01",adInteger
        .Append "col_02",adInteger
        .Append "col_03",adInteger 
		.Append "col_04",adInteger
        .Append "col_05",adInteger
        .Append "col_06",adInteger 
		.Append "col_07",adInteger
        .Append "col_08",adInteger
        .Append "col_09",adInteger 
    End With
    rs.Open
    IF isArray(input_array) THEN
        FOR i = 0 TO UBOUND(input_array,2) -1
            rs.AddNew
            rs("col_01")    = input_array(0,i)
        	rs("col_02")    = input_array(1,i)
       		rs("col_03")    = input_array(2,i)
			rs("col_04")    = input_array(3,i)
            rs("col_05")    = input_array(4,i)
            rs("col_06")    = input_array(5,i)
			rs("col_07")    = input_array(6,i)
            rs("col_08")    = input_array(7,i)
            rs("col_09")    = input_array(8,i)
            rs.Update
        NEXT
    END IF
    
    rs.Sort = "col_01 ASC"
	'rs.Sort = "col_01 DESC"
    SortTheResourceArray = rs.GetRows()
    rs.close
    SET rs = Nothing   
END FUNCTION
		
%>
 
that example makes more sense. I tried your exact code, but got this error.

Code:
ADODB.Fields error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. 

...
...
[b].Append "col_01",adInteger[/b]    <--on this line
 
I kept looking at it, and i didn't include "adovbs.inc" it pulls your example correctly now, I'll see what i can do and let you know.
 
What should i do about this, the top is a line of my array so you see what is going into it.

Array Info
Code:
21,2:00,pm,5:00,pm,Klim,CAD/CAM Workshop,West Building,105B,170

Code:
    With rs.Fields
        .Append "col_01",adInteger
        .Append "col_02",adVarChar, 255 'adInteger
        .Append "col_03",adVarChar, 255 'adInteger
        .Append "col_04",adVarChar, 255 'adInteger
        .Append "col_05",adVarChar, 255 'adInteger
        .Append "col_06",adVarChar, 255 'adInteger
        .Append "col_07",adVarChar, 65534 'adInteger
        .Append "col_08",adVarChar, 255 'adInteger
        .Append "col_09",adInteger
    End With
    rs.Open
    IF isArray(input_array) THEN
        FOR i = 0 TO UBOUND(input_array,2) -1
            rs.AddNew
            rs("col_01")    = input_array(0,i)
            rs("col_02")    = input_array(1,i)
            rs("col_03")    = input_array(2,i)
            rs("col_04")    = input_array(3,i)[b]Subscript out of range: '[number: 3]'[/b]
            rs("col_05")    = input_array(4,i)
            rs("col_06")    = input_array(5,i)
            rs("col_07")    = input_array(6,i)
            rs("col_08")    = input_array(7,i)
            rs("col_09")    = input_array(8,i)
            rs.Update
        NEXT
    END IF
 
Maybe your array isn't as arrayish as you might think.

What do you get when you echo out the dimensions of your array? Post the results:

response.write "width: " & UBOUND(your_array) & "<br />
response.write "height: " & UBOUND(your_array,2)

Also post the results of this:

FOR i = 0 TO UBOUND(your_array)
your_array(i,0) & " | "
NEXT
 
Perhaps the array diminsions are reversed.
Is it 9 wide and X long or X wide and 9 long?

 
That is correct, 3 items width(0-2), 9 elements in each row. The 2 3 2 is correct, that's the first element of each row.

Code:
width: 2
height: 9
2 | 3 | 2 |



below is the array, i added commas when it prints between each row element.

Code:
2,8,am,11,am,Blank,Advanced Composite Veneers (must attend a.m. lecture),West Building,105,168
3,11:45,1:45am,1:30,30pm,,Dental Team Luncheon,Hotel,Regency C-D,147
2,2:00,:00pm,5:00,00pm,Klim,CAD/CAM Workshop,West Building,105B,170
 
So you get subscript out of range when using 3 for the first diminsion because the UBound of the first diminsion is 2.
[tt]
rs("col_01") = input_array(i,0)
rs("col_02") = input_array(i,1)
rs("col_03") = input_array(i,2)
rs("col_04") = input_array(i,3)
rs("col_05") = input_array(i,4)
rs("col_06") = input_array(i,5)
rs("col_07") = input_array(i,6)
rs("col_08") = input_array(i,7)
rs("col_09") = input_array(i,8)
[/tt]

Also if the uBound is zero based and gave a 9 then there are 10 elements?
 
Your array is only three columns wide. How are you building it before sorting?

width: 2 <-- three columns
height: 9 <-- rows

2 | 3 | 2 | <-- this is your first row

Did you run this on your real array?

FOR i = 0 TO UBOUND(your_array)
your_array(i,0) & " | "
NEXT

If it doesn't look like below, your array isn't what you think it is:

2 | 8 | am | 11 | am | Blank|Advanced Composite Veneers (must attend a.m. lecture) | West Building | 105 | 168 |
 
First i want to say thx for sticking with this, most just post 1-2 times then move on.


I tweaked your code to make it display. This is before it calls the function.
Code:
FOR i = 0 TO UBOUND(Course_Array,2)
   response.write Course_Array(0,i) & " | "
NEXT
response.write "<br>DONE"

It outputs this(which are the correct values)
Code:
width: 0
height: 9
2 | 8 | am | 11 | am | Blank | Advanced Composite Veneers (must attend a.m. lecture) | West Building | 105 | 168 |
DONE


It still pulls that error when it gets inside the function. When i was thinking about it, with your simple array it worked, but it was only numbers and you defined the array as 'adInteger' but with more info in the array i used 'adVarChar' and i think that is where the issue is because it craps out when putting the values into the recordset. What should i be using?

Code:
    With rs.Fields
        .Append "col_01",adInteger
        .Append "col_02",adVarChar, 255
        .Append "col_03",adVarChar, 255
        .Append "col_04",adVarChar, 255
        .Append "col_05",adVarChar, 255
        .Append "col_06",adVarChar, 255
        .Append "col_07",adVarChar, 65534
        .Append "col_08",adVarChar, 255
        .Append "col_09",adInteger
    End With
    rs.Open
    IF isArray(input_array) THEN
        FOR i = 0 TO UBOUND(input_array,2) -1
            rs.AddNew
            rs("col_01")    = input_array(0,i)
            rs("col_02")    = input_array(1,i)[b]Error Here[/b]
            rs("col_03")    = input_array(2,i)
            rs("col_04")    = input_array(3,i)
            rs("col_05")    = input_array(4,i)
            rs("col_06")    = input_array(5,i)
            rs("col_07")    = input_array(6,i)
            rs("col_08")    = input_array(7,i)
            rs("col_09")    = input_array(8,i)
            rs.Update
        NEXT
    END IF

The error message is:
Microsoft VBScript runtime error '800a0009'
Subscript out of range: '[number: 1]'
 
Your array isn't what you think it is. You said this is correct:

width: 0
height: 9
2 | 8 | am | 11 | am | Blank | Advanced Composite Veneers (must attend a.m. lecture) | West Building | 105 | 168 |

This means your array looks like this: 1 column wide, 10 rows high.

2
8
am
11
am
Blank
Advanced Composite Veneers (must attend a.m. lecture)
West Building
105
168

Post the output from the loop I sent you, don't tweak it. You are tricking yourself into thinking your array looks like something it isn't.

You tweaked the code to show the first column of the array for as many rows as there are in the array.

Or run this and you'll understand. Post the results.

Code:
my_rows = UBOUND(Course_Array,2)
my_columns = UBOUND(Course_Array,1)
FOR i = 0 TO  my_rows'<--loop for as many rows as there are in the array
response.write "this is row " & i & " | "
  FOR x = 0 TO my_columns
   response.write Course_Array(x,i)
   IF x < my_columns response.write  "|"
  NEXT
  response.write "<br />"
NEXT
response.write "<br /> This is the end of my array. There are " & my_columns + 1 & " columns and " & my_rows + 1 & " rows."


 
Your loop outputs an error, that's why i had to change it: no response.write. Here is your code results from that last post

Code:
width: 0
height: 9
this is row 0 | 2
this is row 1 | 8
this is row 2 | am
this is row 3 | 11
this is row 4 | am
this is row 5 | Blank
this is row 6 | Advanced Composite Veneers (must attend a.m. lecture)
this is row 7 | West Building
this is row 8 | 105
this is row 9 | 168

This is the end of my array. There are 1 columns and 10 rows.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top