Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting multidimensional arrays in VB .NET 1

Status
Not open for further replies.

ctiwari

Technical User
Aug 5, 2003
6
US
Hi

I needed some advice on a fast method to sort data. I am considering two options...

1.
Load the data into a multidimensional array and sort it. In this case, what would be the best method to sort an with approximately 3000 records and 5 fields. I'd like to sort everything based upon one of the fields.

2.
The second option i was thinking about was to import data from a text file into an access database and then sort it. I tried using the following sql code...

"Select field_a, field_b, field_c, field_d, field_e " _
& " INTO mytable IN'" & FileMDB & "' " _
& " FROM " & fileCSV & "SORT BY field_d GROUP BY field_a"

but it gives me an error like 'syntax error in from clause.

Any suggestions will be greatly appreciated!

Thanks

Chet

 
Think about this. One sort method of Array class is
"Array.Sort Method (Array, Array)
Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable interface implemented by each key."

Create a one-dimensional array of strings of the keys on which to sort the arrays by concatenating strings after "fixing" the length of each key and create a companion array that contains the "indexes" to the elements in the unsorted arrays. The "Index" array will be used after the sort to retrieve the unsorted elements in the sorted sequence.
Dim I As integer
Dim aKeys() as String
Dim aIndx() as Integer

For I = 0 To aToSort.Length - 1
' Index to unsorted elements
aIndx(I) = I
' create concatenated key as string
sKey1 = aOld1(I).PadRight(30," "c)
' Turn number into string with leadung zeroes.
sKey2 = System.Convert.ToString(aOld2(I)).Padleft(17,"0"c)
aKeys(I) = sKey1 & sKey2
Next
Sort the pair of arrays in in the sequence of the "keus" array using the "overloaded "Sort Keys" metod
Array.Sort(aKeys, aIndx)
aIndx() is now sorted in the sequence of the keys array. The number in each aIndx element is the index to the corresponding element in the unsorted arrays (still in their original sequence).

Create new arrays from the unsorted arrays using the array of indexes.
dim aNew1(aIndx.Length - 1) as string
dim aNew1(aIndx.Length - 1) as integer
For I = 0 to aIndx.Length - 1
' New array elemnt from element in unsorted array
aNew1(I) = aOld1(aIndx(I))
aNew2(I) = aOld2(aIndx(I))
Next
' Only 4 byte object reference is move
' Not the elements in the entire array
aOld1 = aNew1
aOld2 = aNew2


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top