Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a 4 dimensional array

Posted on 1998-07-06
5
Medium Priority
?
289 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 60 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

971 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