Array ordering

Joined
Jul 1, 2005
Messages
5
Programming Experience
1-3
Hi peeps,

Sorry for the newbie question but i am new to vb.net.
Can someone please tell me how to order an array


I have an array in this structure

0.......1.......2
--------------
7......-1......1
2......-1......2
10....-1......3
29....-3......4
12....-3......5
1......-3......6

how do i order this array by the 0 column

many thanks
 
Just the name of your array ... for instance


VB.NET:
 Dim myArray() AsInteger = {3, 5, 6, 1, 2, 0} 
 
Array.Sort(myArray)
 
MessageBox.Show(myArray(0) & ", " & myArray(1) & ", " & myArray(2) & ", " & myArray(3) & ", " & myArray(4) & ", " & myArray(5)) 
'result would be: 0, 1, 2, 3, 5, 6
 
or:
 
 
[size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] myArray.GetUpperBound(0)

MessageBox.Show(myArray(i))

[/size][size=2][color=#0000ff]Next

[/color][/size]



Cheers ;)


 
Sort multidimensional array

Wait a minute! If you are talking about Sorting Multi-dimensional Arrays then i must say that this method doesn't support that and if you try to pass a multidimensional array into Array.Sort method you will get an exception. That's because .NET Framework doesn't support the sorting of multidimensional arrays. Fortunately, there is a workaround.

The way to approach this problem is to turn a multidimensional array into a jagged array. A jagged array is basically an array of arrays. In other words, the array's element type is also of type array. The key advantage of jagged arrays over multidimensional arrays is that jagged arrays can dynamically scale to infinite dimensions, whereas multidimensional arrays are fixed in their rank, or number of dimensions.

VB.NET:
'a jagged array example
Dim arr1() As Integer = {1, 7}
Dim arr2() As Integer = {12, 25}
Dim arr3() As Integer = {4, 6}
Dim a()() As Integer = {arr1, arr2, arr3}

Also note that multidimensional array comes in two flavors: As i said jagged arrays like those found in C/C++, and rectangular arrays as in Pascal and Visual Basic. Although only rectangular arrays are CLR compliant, both types can be used in .NET ...

Cheers ;)
 
ok, thanks for your help so far, but now i am struggling. this .net seems to be so restrictive.

Say i had a one dimensional array with values from 1 to 150 (not in order)

If i use the array.sort function it sorts it in like this:

10
100
102
..
111
133
..
20
201

this means is sorting by the first number of the integer rather than the whole number. please help me, this just seems ridculous
 
It cannot be ... Sort method sorts values by their total values .. for instance this code below will return/sort values like on the pic below code:

VB.NET:
 Dim myArray() As Integer = {3, 50, 6, 100, 20, 0} 
Array.Sort(myArray)

MessageBox.Show(myArray(0) & ", " & myArray(1) & ", " _

& myArray(2) & ", " & myArray(3) & ", " & myArray(4) & ", " _

& myArray(5), "myArray")


myArray.jpg


Can you send the code how you try to sort the array?

Cheers ;)
 
mystical wurzel said:
ok, thanks for your help so far, but now i am struggling. this .net seems to be so restrictive.

Say i had a one dimensional array with values from 1 to 150 (not in order)

If i use the array.sort function it sorts it in like this:

10
100
102
..
111
133
..
20
201

this means is sorting by the first number of the integer rather than the whole number. please help me, this just seems ridculous
If that is happening then you mustn't have an array of Integer objects, but rather an array of String objects that just look like numbers to the human eye. If you want them to be sorted numerically then they must be numbers. Any Strings will be sorted as strings, even if you know that they're supposed to represent numbers. You can override the default comparison behaviour when sorting if you want to, but it's up to you to code the new behaviour yourself. Otherwise you would have to create a new array and convert each string to a number, then sort that array.
 
Thanks so much for your help so far peeps, im almost there. I just have one more little question about arrays.
After ive ordered my array, all the values shift to the bottom of the array leaving all the balnk values at the top. is there a way to pull all the values to the top or shrink the array to the size of the data.

i.e my array looks like this

0,0,0,0,0,0,1,2,3,4,5

0 need to be lost or made like this

1,2,3,4,5,0,0,0,0,0,0


Once again thanks for your help
 
You could use one of the overloads of Array.Copy to remove the zeroes or you could use one of the overloads of Array.Sort that takes an IComparer to do a custom sort where zero is greater than all other values.
 
Back
Top