arrange arrays in increasing order

G

Guest

I have array of arrays. I need to arrange them in an increased order of
their element 0

Here is what i want.
dim InputArray()() = {{2,0,0},{1,0,0},{6,0,0},{3,0,0}}

and this is the output
outputArray()() = {{1,0,0},{2,0,0},{3,0,0},{6,0,0}}

Has any body got any idea how to it.

TIA
nafri
 
H

Herfried K. Wagner [MVP]

nafri said:
I have array of arrays. I need to arrange them in an increased order of
their element 0

Here is what i want.
dim InputArray()() = {{2,0,0},{1,0,0},{6,0,0},{3,0,0}}

and this is the output
outputArray()() = {{1,0,0},{2,0,0},{3,0,0},{6,0,0}}

Has any body got any idea how to it.

Untested (written from scratch):

\\\
Imports System.Collections
..
..
..
Dim InputArray()() As Integer = _
New Integer()() { _
New Integer() {2, 0, 0}, _
New Integer() {1, 0, 0}, _
New Integer() {6, 0, 0}, _
New Integer() {3, 0, 0} _
}
Array.Sort(InputArray, New FooComparer)
..
..
..
Public Class FooComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compare
Dim xx As Integer() = DirectCast(x, Integer())
Dim yy As Integer() = DirectCast(x, Integer())
Return yy(0) - xx(0)
End Function
End Class
///
 
G

Guest

thanks, it works well, but arranges them in decreasing order. Although i can
reverse and get it what i want, but is their anything that i can do in the
compare function to make it arrange in decreasing order.

nafri
 
H

Herfried K. Wagner [MVP]

nafri said:
thanks, it works well, but arranges them in decreasing order. Although i
can
reverse and get it what i want, but is their anything that i can do in the
compare function to make it arrange in decreasing order.

=> 'Return xx(0) - yy(0)
 
J

Jay B. Harlow [MVP - Outlook]

nafri,
Have you tried:

Return xx(0) - yy(0)

Rather then compare y to x, compare x to y.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top