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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting two dimentional array

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I have an Array that is 3 columns wide (and however many rows) each have a HeadID, URL, Section<br><br>I want to sort the Array First by HeadID then by Section, for example I have this...<br><br><FONT FACE=monospace><br>3-2&nbsp;&nbsp;¦ Some URL ¦ 1<br>3-7&nbsp;&nbsp;¦ Some URL ¦ 1<br>4-13 ¦ Some URL ¦ 1<br>3-2&nbsp;&nbsp;¦ Some URL ¦ 2<br>3-7&nbsp;&nbsp;¦ Some URL ¦ 2<br>4-13 ¦ Some URL ¦ 2<br>3-2&nbsp;&nbsp;¦ Some URL ¦ 3<br>3-7&nbsp;&nbsp;¦ Some URL ¦ 3<br>4-13 ¦ Some URL ¦ 3<br></font><br><br>I want to turn it into this<br><br><FONT FACE=monospace><br>3-2&nbsp;&nbsp;¦ Some URL ¦ 1<br>3-2&nbsp;&nbsp;¦ Some URL ¦ 2<br>3-2&nbsp;&nbsp;¦ Some URL ¦ 3<br>3-7&nbsp;&nbsp;¦ Some URL ¦ 1<br>3-7&nbsp;&nbsp;¦ Some URL ¦ 2<br>3-7&nbsp;&nbsp;¦ Some URL ¦ 3<br>4-13 ¦ Some URL ¦ 1<br>4-13 ¦ Some URL ¦ 2<br>4-13 ¦ Some URL ¦ 3<br></font><br><br>Originally, since I had only one number at the head ID (the URL table has a single number for it's reference) it would go in proper order, but now since multiple rows may have same first number, I end up getting first page of every head , then second, then third, instead of 1 2 3 of that specific head, so I need to figure out how to sort it in that two ways, first by Head, then by Section. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
I have this so far for sorting just the head IDs<br>(you may reconize this as a bubblesort)<br><br><FONT FACE=monospace><font color=blue><br>Change = True<br>RootNum = 0<br>While Change = True and RootNum &lt; UBound(URLList)<br>&nbsp;HighestIndex = &quot;0-0&quot;<br>&nbsp;Change = False<br>&nbsp;For J = RootNum to UBound(URLList)<br>&nbsp;&nbsp;rtn = Instr(1,URLList(j,0), &quot;-&quot;)<br>&nbsp;&nbsp;rta = Instr(1,HighestIndex, &quot;-&quot;)<br>&nbsp;&nbsp;if rtn &gt; 0 then<br>&nbsp;&nbsp;&nbsp;if Left(URLList(j,0),rtn-1) &gt; Left(HighestIndex, rta-1) then<br>&nbsp;&nbsp;&nbsp;&nbsp;Change = True<br>&nbsp;&nbsp;&nbsp;&nbsp;HighestIndex = URLList(j,0) <br>&nbsp;&nbsp;&nbsp;&nbsp;HIndex = j<br>&nbsp;&nbsp;&nbsp;elseif Left(URLList(j,0),rtn-1) = Left(HighestIndex, rta-1) then<br>&nbsp;&nbsp;&nbsp;&nbsp;if Right(URLList(j,0),Len(URLList(j,0)) - rtn) &gt; Right(HighestIndex,Len(HighestIndex) - rta) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Change = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HighestIndex = URLList(j,0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HIndex = j<br>&nbsp;&nbsp;&nbsp;&nbsp;end if<br>&nbsp;&nbsp;&nbsp;end if<br>&nbsp;&nbsp;end if<br>&nbsp;next<br>&nbsp;if Change = true then<br>&nbsp;&nbsp;TmpOne = URLList(RootNum,0)<br>&nbsp;&nbsp;TmpTwo = URLList(RootNum,1)<br>&nbsp;&nbsp;TmpThree = URLList(RootNum,2)<br>&nbsp;&nbsp;URLList(RootNum,0) = URLList(HIndex,0)<br>&nbsp;&nbsp;URLList(RootNum,1) = URLList(HIndex,1)<br>&nbsp;&nbsp;URLList(RootNum,2) = URLList(HIndex,2)<br>&nbsp;&nbsp;URLList(HIndex,0) = TmpOne<br>&nbsp;&nbsp;URLList(HIndex,1) = TmpTwo<br>&nbsp;&nbsp;URLList(HIndex,2) = TmpThree<br>&nbsp;&nbsp;RootNum = RootNum + 1<br>&nbsp;end if<br>Wend<br></font></font> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Ok I got it, I just created another if then if its = (I'll highlight it.<br><br><FONT FACE=monospace><font color=blue><br>Change = True<br>RootNum = 0<br>While Change = True and RootNum &lt; UBound(URLList)<br>&nbsp;HighestIndex = &quot;0-0&quot;<br>&nbsp;Change = False<br>&nbsp;For J = RootNum to UBound(URLList)<br>&nbsp;&nbsp;rtn = Instr(1,URLList(j,0), &quot;-&quot;)<br>&nbsp;&nbsp;rta = Instr(1,HighestIndex, &quot;-&quot;)<br>&nbsp;&nbsp;if rtn &gt; 0 then<br>&nbsp;&nbsp;&nbsp;if Left(URLList(j,0),rtn-1) &gt; Left(HighestIndex, rta-1) then<br>&nbsp;&nbsp;&nbsp;&nbsp;Change = True<br>&nbsp;&nbsp;&nbsp;&nbsp;HighestIndex = URLList(j,0) <br>&nbsp;&nbsp;&nbsp;&nbsp;HIndex = j<br>&nbsp;&nbsp;&nbsp;elseif Left(URLList(j,0),rtn-1) = Left(HighestIndex, rta-1) then<br>&nbsp;&nbsp;&nbsp;&nbsp;if Right(URLList(j,0),Len(URLList(j,0)) - rtn) &gt; Right(HighestIndex,Len(HighestIndex) - rta) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Change = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HighestIndex = URLList(j,0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HIndex = j<br><font color=red>&nbsp;&nbsp;&nbsp;&nbsp;elseif Right(URLList(j,0),Len(URLList(j,0)) - rtn) = Right(HighestIndex,Len(HighestIndex) - rta) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if URLList(j,2) &lt; URLList(HIndex,2) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Change = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HighestIndex = URLList(j,0) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HIndex = j<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end if<br>&nbsp;&nbsp;&nbsp;&nbsp;end if</font><br>&nbsp;&nbsp;&nbsp;end if<br>&nbsp;&nbsp;end if<br>&nbsp;next<br>&nbsp;if Change = true then<br>&nbsp;&nbsp;TmpOne = URLList(RootNum,0)<br>&nbsp;&nbsp;TmpTwo = URLList(RootNum,1)<br>&nbsp;&nbsp;TmpThree = URLList(RootNum,2)<br>&nbsp;&nbsp;URLList(RootNum,0) = URLList(HIndex,0)<br>&nbsp;&nbsp;URLList(RootNum,1) = URLList(HIndex,1)<br>&nbsp;&nbsp;URLList(RootNum,2) = URLList(HIndex,2)<br>&nbsp;&nbsp;URLList(HIndex,0) = TmpOne<br>&nbsp;&nbsp;URLList(HIndex,1) = TmpTwo<br>&nbsp;&nbsp;URLList(HIndex,2) = TmpThree<br>&nbsp;&nbsp;RootNum = RootNum + 1<br>&nbsp;end if<br>Wend<br></font></font> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top