# Sorting array names by value in array

### Question

• I am using Studio 2005 with visual basic 2005I

I need to sort arrays in such away as the arrays themselves are sorted by a value contained in one element.

I have several arrays

myarray1(9)

myarray2(9)

myarray3(9)

myarray4(9)

myarray5(9)

etc.

I can sort the arrays on value in one element, but this just gives a list of those elements in the sort order.

I need to actually sort the arrays by name based on that same order

Example if myarray1 total is 44

myarray2 total is 66

myarray3 total is 34

myarray4 total is 22

myarray5 total is 88

sorting by code on the totals gives me 22,34,44,66,88 with no clue as to which is which without going into the code during debuging.

What I need is Array4 = 22

Array3=34

Array1=44

Array2=66

Array5=88

What would be best is after sorting, I just got list of arrays by name in order: Array4, Array3,Array1,Array2,Array5, etc.

I need to add a number to each array based on which has the lowest or highest total.  If highest, then that array would get next low number.

Object is to have all arrays approximately even after all numbers are added.

I have added an element ot each array and programed in the array number right in the array, but can't figure out how to use it or if I should.

Can anyone help?  Maybe it can't be done in visual basic 2005

Thursday, December 14, 2006 9:59 PM

• Hi,

Have you considered using a 2 dimension array instead?

Another possibility is to add the arrays to another array and to sort that array:

dim myNewArray() = { Array1, Array2, Array3...}

myNewArray.Sort(MyCompareMethod)

public function MyCompareMethod(arr1() as Integer, arr2() as Integer) as Integer

//calculate sum of arr1
int sum1 = ' ...
int sum2  = '...

return sum1.CompareTo(sum2)

end function

With the implementation above, the sum of each array is calculated multiple times so it would be better to cache that value.

Charles

Friday, December 15, 2006 3:29 AM

### All replies

• Hi,

Have you considered using a 2 dimension array instead?

Another possibility is to add the arrays to another array and to sort that array:

dim myNewArray() = { Array1, Array2, Array3...}

myNewArray.Sort(MyCompareMethod)

public function MyCompareMethod(arr1() as Integer, arr2() as Integer) as Integer

//calculate sum of arr1
int sum1 = ' ...
int sum2  = '...

return sum1.CompareTo(sum2)

end function

With the implementation above, the sum of each array is calculated multiple times so it would be better to cache that value.

Charles

Friday, December 15, 2006 3:29 AM
• I am useing a 2 dimension array. I tried your idea of making an array of the arrays, but it didn't work for me.  Instead, I worked up a little routien that seaches the array's total element and find the highest total and add the new number to that array, marked than array as used with an X in an unused element to  show that array has been used and find the next highest, etc.  It seems like more code is needed this way, but it does work.

I really appreciated your offer of help.

Monday, December 18, 2006 2:33 AM