Solved

Sorting a 4 dimensional array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
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…

911 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

25 Experts available now in Live!

Get 1:1 Help Now