Sorting Multi-dimensional Arrays

Ikatiar

New member
Joined
Nov 6, 2005
Messages
3
Programming Experience
1-3
I have an Array similiar to

Dim PersonNameAge(1,4) As String

PersonNameAge(0,0) = "Chris"
PersonNameAge(0,1) = "Jason"
PersonNameAge(0,2) = "Shawn"
PersonNameAge(0,3) = "Pete"
PersonNameAge(0,4) = "Keith"

PersonNameAge(1,0) = "28"
PersonNameAge(1,1) = "21"
PersonNameAge(1,2) = "26"
PersonNameAge(1,3) = "29"
PersonNameAge(1,4) = "23"


Is there a way I can sort the array by Name, but keep the corrisponding age correct?

Thanks a ton.
 
Unless you have a specific reason for using a multi-dimensional array you should be using a SortedList in this situation, which will sort the names automatically without any intervention from you and maintain the name:age correspondence, as well as provide more efficient access than an array.
 
Not sure if the sortedlist will work for me because I have many entries.

I have multiple entries that look something like

FName, LName, SSNum, DHired, Wage

If I had 20 records, how could I sort by SSNum, or DHired etc etc.

Thanks for all the feedback.

Chris
 
In that case it sounds like you should be using a DataTable. A DataTable has a DefaultView property, which is a DataView for the table. A DataView has a Sort property which allows you to sort your data by any column in either direction. To sort in descending order by SSNum you would set the Sort property to "SSNum DESC". Note that a DataView is a view of the data and the actual data is unaffected, i.e. if you sort a DataView the original DataTable is unaffected. The DataView also has a RowFilter property that allows you to include and exclude various rows based on their values in certain columns. Again, the actual DataTable is unaffected. I suggest you check out the DataTable.DefaultView property in the help/MSDN.

Even apart from the sorting part, data like this is generally stored in a DataTable. Did it come from a database in the first place?
 
Awesome, this helps a ton!
Yes, origionally I am pulling the data from a database.

Thanks guys for all your help
Chris
 
If you are getting your data from a database in the first place then getting it into a DataTable is basically a one-line affair. Depending on the circumstances you may be able to do it more efficiently (read: faster) with some additonal code but it is still relatively simple.
 
Back
Top