|
-
Jun 7th, 2009, 09:23 PM
#1
Thread Starter
Member
[RESOLVED] Array Sorting Help...
Hi people.. 
Let's say I have a array named st with a set of values.. 
I want to sort the st(i,3) values from the highest to the lowest.
From what I heard, there is method call Array.Sort(ArrayName)
However this method is only applicable to one dimension array.. 
Is there a method in visual studio that can do the sorting for multi dimension array? 
An Example of what I want to do is listed down below.. 
Before Sorting.
Code:
Dim st As String(,) = _
{ _
{"", "CA", "CA 1.1", "1505"}, _
{"", "CA 1.1", "CA 1.2", "1311"}, _
{"", "CA 1.2", "CA 1.3", "1168"}, _
{"", "CA 1.3", "Expo", "572"}, _
{"", "CA 1.3", "CA 2.1", "351"}, _
{"", "CA 2.1", "CA 2.2", "682"}, _
{"", "PR", "PR 1.1", "2436"} _
}
After Sorting..
Code:
Dim st As String(,) = _
{ _
{"", "PR", "PR 1.1", "2436"}, _
{"", "CA", "CA 1.1", "1505"}, _
{"", "CA 1.1", "CA 1.2", "1311"}, _
{"", "CA 1.2", "CA 1.3", "1168"}, _
{"", "CA 2.1", "CA 2.2", "682"}, _
{"", "CA 1.3", "Expo", "572"}, _
{"", "CA 1.3", "CA 2.1", "351"} _
}
Last edited by LawrenceWong; Jun 7th, 2009 at 09:49 PM.
-
Jun 7th, 2009, 10:12 PM
#2
Re: Array Sorting Help...
There is no inbuilt way to sort a 2D array. It would have to be all manual and it would be a bit messy. Basically, if you feel you need to sort a 2D array then you shouldn't be using a 2D array. This is one of those situations. 2D arrays are intended to be used for matrices, where every element is equivalent. That is not so in your case. You are using each "row" in the array as a pseudo-object. What you should be doing is defining your own type (class or structure) to represent that object and then create a 1D array of instances of that type. It's then child's play to sort that 1D array.
For instance, you might define a type like this:
vb.net Code:
Friend Structure SomeType
Public Field1 As String
Public Field2 As String
Public Field3 As String
Public Field4 As String
End Structure
You could then replace that code that creates your 2D array with this:
vb.net Code:
Dim st As SomeType() = _
{ _
New SomeType With {.Field1 = "", .Field2 = "CA", .Field3 = "CA 1.1", .Field4 = "1505"}, _
New SomeType With {.Field1 = "", .Field2 = "CA 1.1", .Field3 = "CA 1.2", .Field4 = "1311"}, _
New SomeType With {.Field1 = "", .Field2 = "CA 1.2", .Field3 = "CA 1.3", .Field4 = "1168"}, _
New SomeType With {.Field1 = "", .Field2 = "CA 1.3", .Field3 = "Expo", .Field4 = "572"}, _
New SomeType With {.Field1 = "", .Field2 = "CA 1.3", .Field3 = "CA 2.1", .Field4 = "351"}, _
New SomeType With {.Field1 = "", .Field2 = "CA 2.1", .Field3 = "CA 2.2", .Field4 = "682"}, _
New SomeType With {.Field1 = "", .Field2 = "PR", .Field3 = "PR 1.1", .Field4 = "2436"} _
}
Note that that code could be shortened if SomeType had a constructor because you wouldn't need to specify each file name each time.
Now that you have a 1D array you can use Array.Sort to sort it. Because each element is an object, when you switch two elements based on the values of one field, all the other values automatically get switched too, so there's no need for lots of manual swapping of values. In this case, if you want to sort by Field4 values you can simply do this:
vb.net Code:
Array.Sort(st, Function(x, y) x.Field4.CompareTo(y.Field4))
It may not be immediately obvious how that's working because it's using a lambda expression to implicitly create a Comparison delegate. A more expanded version of the same might look like the following. First, you'd define a method to compare two SomeType object on their Field4 values and return an Integer that indicated their relative order. The convention is that 0 means equivalent, less than o means the first value comes first and greater than 0 means the second value comes first:
vb.net Code:
Private Function CompareSomeTypesByField4(ByVal x As SomeType, ByVal y As SomeType) As Integer
Return x.Field4.CompareTo(y.Field4)
End Function
Hopefully you can see that that's fairly similar to the second argument to Array.Sort above. Now, you can use that method to sort the array by creating a Comparison delegate either implicitly:
vb.net Code:
Array.Sort(st, AddressOf CompareSomeTypesByField4)
or explicitly:
vb.net Code:
Array.Sort(st, New Comparison(Of SomeType)(AddressOf CompareSomeTypesByField4))
The final point to note is that that is going to sort the Field4 values as text rather than as numbers, because Field4 has been declared type String. Now that you're using your own type you can declare each property or field as whatever type you want. If Field4 contains numbers then it would probably be more appropriate to declare it as a numerical type, e.g. Integer. Array.Sort will then treat the values as numbers and, instead of "10" coming before "2", 2 will come before 10.
-
Jun 7th, 2009, 10:41 PM
#3
Thread Starter
Member
Re: Array Sorting Help...
Hi there..
Thanks for replying..
Your method worked..
However there is a problem..
I wanted to display the list of values being sorted in a list box..
However it only show in the list box
ProjectName.FormName + SomeType
Any idea on why this might occur instead of the values displaying? 
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Array.Sort(test, New Comparison(Of SomeType)(AddressOf CompareSomeTypesByField4))
Try
For i = 0 To test.Length
ListBox1.Items.Add(test(i))
Next i
Catch ex As Exception
End Try
End Sub
-
Jun 7th, 2009, 11:29 PM
#4
Re: Array Sorting Help...
First up, don't use names like "SomeType" and "Field4". They were just examples I used because I didn't know what your data actually represented. As always, you should be using descriptive names for all your types and members.
As for the question, when you add an object to the Items of a ListBox, ComboBox or the like, by default its ToString method will be called to get a String to display in the control. You haven't overridden the ToString method of your type so it will exhibit the default behaviour and return the type name. You can override the ToString method in your type definition and have it return whatever value you want displayed, e.g. a combination of all the fields or just one field.
To override a member you should let Intellisense help you. In the body of the type you simply type overrides followed by a space and Intellisense will display a list of members that can be overridden. You select the one you want and a stub will be generated. You can then just add the body.
Just keep in mind that this means that any time you call ToString on an instance of your type it will invoke this override.
If you only want to display the value of one property in the control then an alternative is to set the DisplayMember of the control to the name of that property. Just note that this requires you to implement properties in your type though, rather than fields, because only properties can be bound.
-
Jun 8th, 2009, 12:10 AM
#5
Thread Starter
Member
Re: Array Sorting Help...
I see..
Thanks for your help..
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|