Solved

Sorting a 4 dimensional array

Posted on 1998-07-06
5
217 Views
Last Modified: 2010-05-03
My program reads a text file with data in 4 columns.

I need to sort the file based on the data in the first column. My program already uses a listbox for other things and I'd prefer to use another listbox for this matter too.
I load all the data into a 4 dimensional array and I tried putting the data into an indexed listbox with the sort set to True. The problem is that it sorts all 4 dimensions and not only the first one, meaning that the data is mixed up.

Is there an easy way of sorting this array?

Any help is appreciated.
Teddy
0
Comment
Question by:tflarsen
  • 2
  • 2
5 Comments
 

Expert Comment

by:peterprice
ID: 1465033
Try Using  a Grid instead. Place alll 4 colums of data in a grid and then load your list box with the Data from The first Grid Column and the row number into the index. and then sort the list box.
0
 

Author Comment

by:tflarsen
ID: 1465034
I know that solution already, but I'm not using the grid control anywhere else in my program and I'm trying to make this program as small as possible.

Do you know any other solutions?
0
 
LVL 6

Expert Comment

by:alamo
ID: 1465035
Place only the column you wish to sort on into the ListBox. Set each item's ItemData to its original index in the array. That way you can know which entry in the array is refererd to by each entry in the ListBox.

Hope this helps... ask if you want a code sample.

0
 

Author Comment

by:tflarsen
ID: 1465036
I'd like the example please. I think I know what you are thinking about but I'd like to be 100% sure about it.

Thnx so far.

I'm going to reopen the question so you can get the points.
0
 
LVL 6

Accepted Solution

by:
alamo earned 30 total points
ID: 1465037
Thanks, tflarsen.

First off - this isn't really a 4-dimensional array, is it? I would think you could read lines of data 4 columns wide into a 2-dimensional array, i.e. if you have 100 lines you need to dimension the array (99,3) (if you want to use the 0th element, 100,4 if you don't).

I will assume, then, you are storing the data in a 2-dimensional string array. That's the most natural way to store the data - I will assume that's it, if it isn't let me know and I'll adjust the code.

' The 4 columns for each line i are in ColData(i, 0),ColData(i, 1),ColData(i, 2) and ColData(i, 3)
' Put the column to sort by into the listbox:
For i = 0 To UBound(ColData, 1)
 List1.AddItem ColData(i, 0)
 List1.ItemData(List1.NewIndex) = i
Next

' Create a new array with the sorted data
ReDim sortedColData(UBound(ColData, 1), UBound(ColData, 2)) As String
For i = 0 To List1.ListCount - 1
 k = List1.ItemData(i)
 For j = 0 To UBound(ColData, 2)
  sortedColData(i, j) = ColData(k, j)
 Next
Next

' Now sortedColData is a sorted version of ColData

Hope this helps!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now