Current location: Hot Scripts Forums » Programming Languages » Visual Basic » Sort Multidimensional arrays


Sort Multidimensional arrays

Reply
  #1 (permalink)  
Old 06-27-08, 01:25 AM
LLWoody LLWoody is offline
New Member
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Sort Multidimensional arrays

Does anyone have a function coded to sort a multidimensional array? I found one by Eric Repec-InetSolution but it didnt work. I dont want to recreate the wheel. thanks all!
Reply With Quote
  #2 (permalink)  
Old 06-28-08, 01:52 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Have you tried this one yet?

http://www.freevbcode.com/ShowCode.Asp?ID=2530


Pete
__________________
Reply With Quote
  #3 (permalink)  
Old 06-30-08, 08:04 PM
LLWoody LLWoody is offline
New Member
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks! i'll try that.
Reply With Quote
  #4 (permalink)  
Old 07-01-08, 08:08 PM
LLWoody LLWoody is offline
New Member
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Visual Basic 2D Array Sort

well it worked with a few tweaks. here's my updated version if anyone else needs it:
vb Code:
  1. Public Sub SortArray(ByRef DArray(), Element As Integer)
  2.     Dim gap As Integer, doneflag As Integer, SwapArray()
  3.     Dim Index As Integer
  4.     Dim acol As Integer
  5.     Dim cnt As Integer
  6.        
  7.     ReDim SwapArray(2, UBound(DArray, 1), UBound(DArray, 2))
  8.     'Gap is half the records
  9.     gap = Int(UBound(DArray, 2) / 2)
  10.     gap = gap + IIf(UBound(DArray, 2) Mod 2, 1, 0)
  11.     Do While gap >= 1
  12.         Do
  13.             doneflag = 1
  14.             'For Index = 0 To (UBound(DArray, 2) - (gap + 1))
  15.             For Index = 0 To (UBound(DArray, 2) - (gap))
  16.                 'Compare 1st 1/2 to 2nd 1/2
  17.                 If DArray(Element, Index) > DArray(Element, (Index + gap)) Then
  18.                     For acol = 0 To (UBound(SwapArray, 2))
  19.                         'Swap Values if 1st > 2nd
  20.                         SwapArray(0, acol, Index) = DArray(acol, Index)
  21.                         SwapArray(1, acol, Index) = DArray(acol, Index + gap)
  22.                     Next
  23.                     For acol = 0 To (UBound(SwapArray, 2))
  24.                         'Swap Values if 1st > 2nd
  25.                         DArray(acol, Index) = SwapArray(1, acol, Index)
  26.                         DArray(acol, Index + gap) = SwapArray(0, acol, Index)
  27.                     Next
  28.                     cnt = cnt + 1
  29.                     doneflag = 0
  30.                 End If
  31.             Next
  32.         Loop Until doneflag = 1
  33.         gap = Int(gap / 2)
  34.     Loop
  35. End Sub

Last edited by Nico; 07-02-08 at 02:27 AM. Reason: Wrappers.
Reply With Quote
  #5 (permalink)  
Old 07-02-08, 08:01 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Great. Thanks for posting your updated code.


Pete
__________________
Reply With Quote
  #6 (permalink)  
Old 10-17-08, 06:35 AM
travimca travimca is offline
New Member
 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
VB6 2 dimension Array sorting

Quote:
Originally Posted by digioz View Post
Great. Thanks for posting your updated code.


Pete
Hi,
I having 2 dimension array. My data is looks like this

20.44002824, Sampletext
19.39352222, aaa
19.67565656, bbb

I want to sort the column 1, i need a output like this

20.44002824, Sampletext
19.67565656, bbb
19.39352222, aaa

Can you please help me. I dont know how to do this.
Reply With Quote
  #7 (permalink)  
Old 08-18-11, 08:09 AM
thydzik thydzik is offline
New Member
 
Join Date: Aug 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Great post digioz. My only change would be to change the Integer types to Long, as this is actually faster.

travimca,

run it as:
SortArray yourarray, 1
(assuming you index starts at 0)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# Multidimensional Arrays Length Barnz1986 Windows .NET Programming 3 05-23-06 11:20 PM
work on arrays within arrays - i'm stuck... nassau PHP 1 04-29-06 02:37 PM
Arrays with sub arrays perleo PHP 1 09-30-05 07:15 PM
Can record arrays on database? mhs12grade1992 PHP 5 02-17-05 11:20 AM
Problem with a sort table js function tdubyou JavaScript 0 05-03-04 09:19 AM


All times are GMT -5. The time now is 01:18 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.