Solved

Sorting a 4 dimensional array

Posted on 1998-07-06
5
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 130
to transfer string from C lanaguage to VBA 4 85
How does CurrentUser work? 10 44
Publisher:   Unknown     VB.exe Application 1 31
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

734 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