|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Array.SortPublic Structure Identity Public firstname As String Public name As String End Structure I declared a array of a structure : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Arr(0 To 3) As Identity Arr(0).firstname = "zzzzzzz" Arr(1).firstname = "aaaa" Arr(2).firstname = "sss" Arr(3).firstname = "mmmm" Arr(0).name = "uuuu" Arr(1).name = "ffff" Arr(2).name = "zzzz" Arr(3).name = "aaaa" End Sub End Class Is it possible to sort this one dimensional array by 'firstname' or by 'name' ? Using array.sort ? But with which arguments ? Thanks for response. Philip Module Module1
Sub Main() Dim Arr(0 To 3) As Identity Arr(0).firstname = "zzzzzzz" Arr(1).firstname = "aaaa" Arr(2).firstname = "sss" Arr(3).firstname = "mmmm" Arr(0).name = "uuuu" Arr(1).name = "ffff" Arr(2).name = "zzzz" Arr(3).name = "aaaa" Dim sorter As New ListViewSortOrder() Array.Sort(Arr, sorter) For Each PER As Identity In Arr Console.WriteLine(PER.firstname & ControlChars.Tab & PER.name) Next Console.Read() End Sub End Module Public Structure Identity Public firstname As String Public name As String End Structure Public Class ListViewSortOrder Implements IComparer Public Sub New() End Sub ''' <summary> ''' Comparision function for IComparer ''' </summary> ''' <param name="x"></param> ''' <param name="y"></param> ''' <returns></returns> ''' <remarks></remarks> Public Function compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim xItem, yItem As Identity Dim sortOrder As String = "ASC" xItem = CType(x, Identity) yItem = CType(y, Identity) If sortOrder = "ASC" Then Return (xItem.firstname).CompareTo(yItem.firstname) Else Return (yItem.firstname).CompareTo(xItem.firstname) End If End Function End Class Thanks, sincerely. I take this way.
Philip "Brian Henry" <nospam@nospam.com> a écrit dans le message de news: OhoF6WsQGHA.2***@TK2MSFTNGP15.phx.gbl...Show quoteHide quote > Module Module1 > > Sub Main() > > Dim Arr(0 To 3) As Identity > > Arr(0).firstname = "zzzzzzz" > > Arr(1).firstname = "aaaa" > > Arr(2).firstname = "sss" > > Arr(3).firstname = "mmmm" > > Arr(0).name = "uuuu" > > Arr(1).name = "ffff" > > Arr(2).name = "zzzz" > > Arr(3).name = "aaaa" > > > > Dim sorter As New ListViewSortOrder() > > Array.Sort(Arr, sorter) > > For Each PER As Identity In Arr > > Console.WriteLine(PER.firstname & ControlChars.Tab & PER.name) > > Next > > Console.Read() > > End Sub > > End Module > > > > Public Structure Identity > > Public firstname As String > > Public name As String > > End Structure > > > > Public Class ListViewSortOrder > > Implements IComparer > > > Public Sub New() > > End Sub > > > > ''' <summary> > > ''' Comparision function for IComparer > > ''' </summary> > > ''' <param name="x"></param> > > ''' <param name="y"></param> > > ''' <returns></returns> > > ''' <remarks></remarks> > > Public Function compare(ByVal x As Object, ByVal y As Object) As Integer > Implements IComparer.Compare > > Dim xItem, yItem As Identity > > Dim sortOrder As String = "ASC" > > xItem = CType(x, Identity) > > yItem = CType(y, Identity) > > > > If sortOrder = "ASC" Then > > Return (xItem.firstname).CompareTo(yItem.firstname) > > Else > > Return (yItem.firstname).CompareTo(xItem.firstname) > > End If > > End Function > > End Class > > "philip" <p***@philippe.com> schrieb: You'll have to implement your own comparer:> Is it possible to sort this one dimensional array by 'firstname' or by > 'name' ? > Using array.sort ? But with which arguments ? <URL:http://dotnet.mvps.org/dotnet/samples/techniques/CompareOperator.zip> -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Thanks
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> a écrit dans le message de news: OKKpcYsQGHA.5***@TK2MSFTNGP09.phx.gbl...Show quoteHide quote > "philip" <p***@philippe.com> schrieb: >> Is it possible to sort this one dimensional array by 'firstname' or by >> 'name' ? >> Using array.sort ? But with which arguments ? > > You'll have to implement your own comparer: > > <URL:http://dotnet.mvps.org/dotnet/samples/techniques/CompareOperator.zip> > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/>
Adding button programatically - NON-FUNCTIONAL!!
Checking for new versions in a setup project Garbage collection and callbacks from unmanaged code. Exception Error When Calling Procedure Multiple Times Can a console VB.NET prog return a value to caller? Help with strings. Error on Graphics CompositingMode.SourceCopy Numeric Variables converting from combobox.text to long() Highlight in Tree view |
|||||||||||||||||||||||