Solved

Sorting a 4 dimensional array

Posted on 1998-07-06
5
238 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 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

17 Experts available now in Live!

Get 1:1 Help Now