Sort an array of structures in .NET

前端 未结 7 1194
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 07:02

This is one of those times when only the hive mind can help - no amount of Google-fu can!

I have an array of structures:

Structure stCar 
    Dim Nam         


        
相关标签:
7条回答
  • 2020-12-16 07:08

    Another possibility, that doesn't use Linq but instead uses the .Net Array class' Sort method:

    Module Module1
        Structure stCar
            Dim Name As String
            Dim MPH As String
    
            Sub New(ByVal _Name As String, ByVal _MPH As Integer)
                Name = _Name
                MPH = _MPH
            End Sub
        End Structure
    
        Class CarCompareMph : Implements IComparer
    
            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
                Dim xCar As stCar = DirectCast(x, stCar)
                Dim yCar As stCar = DirectCast(y, stCar)
                Return New CaseInsensitiveComparer().Compare(xCar.MPH, yCar.MPH)
            End Function
        End Class
    
        Sub Main()
            Dim cars() As stCar = {New stCar("honda", 50), New stCar("ford", 10)}
            Array.Sort(cars, New CarCompareMph)
    
            For Each c As stCar In cars
                Console.WriteLine("{0} - {1} MPH", c.Name, c.MPH)
            Next
        End Sub
    
    End Module
    

    I'm not sure if that's what you're looking for, but it's another approach.

    0 讨论(0)
  • 2020-12-16 07:15

    In VB.Net of VS2015 there is another method of sorting:

    cars.Sort(New Comparison(Of stCar)(Function(x, y) 'x and y are of type of stCar
                                                                 If x.MPH > y.MPH Then
                                                                     Return 1 ' Return Value>0 means x>y
                                                                 End If
                                                                 If x.MPH < y.MPH Then
                                                                     Return -1 ' Return Value<0 means x<y
                                                                 End If
                                                                 If x.MPH = y.MPH Then
                                                                     Return 0 ' Return Value=0 means x=y
                                                                 End If
                                                             End Function))
    
    0 讨论(0)
  • 2020-12-16 07:24

    I don't know VB.NET, but you should be able to do this my implimenting IComparer. Take a look at this example

    http://www.java2s.com/Code/VB/Data-Structure/UseIComparertosortbydifferentproperties.htm

    Alternativly you can also use Linq

    0 讨论(0)
  • 2020-12-16 07:26

    The easiest way to perform the sort would be to use LINQ to Objects.

    Dim q = From c In cars Order By c.MPH Select c
    
    0 讨论(0)
  • 2020-12-16 07:31

    Assuming that the structure has a property called MPH:

    cars = cars.OrderBy(Function(c) c.MPH)
    

    Note: the above code was auto-converted from the following c# code (in case it contains errors):

    cars = cars.OrderBy(c => c.MPH);
    
    0 讨论(0)
  • 2020-12-16 07:31

    A simple way that seems to be working for me in vb.net 2013 is as follows:

    cars.Sort(Function(c1,c2) c1.MPH.CompareTo(c2.MPH))
    
    0 讨论(0)
提交回复
热议问题