6

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 Name As String
    Dim MPH As Integer

    Sub New(ByVal _Name As String, ByVal _MPH As Integer)
        Name = _Name
        MPH = _MPH
    End Sub
End Structure

How do I sort the array on one variable / property of the structure?

Dim cars() as stCar = {new stCar("ford",10), new stCar("honda",50)}

cars.sort("MPH") // how do I do this?
0

7 Answers 7

16

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);
Sign up to request clarification or add additional context in comments.

1 Comment

this is what worked for me: cars = cars.OrderBy(Function(c) c.MPH).ToArray
5

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

Comments

2

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

1 Comment

You can't implement IComparer against structures. I believe it has to be implemented against a class, and structures are not classes in VB.NET
1

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.

1 Comment

Applying a lexicographical sort order to a collection of numeric values isn't going to do what you want. It might work with most data sets, but you'll be surprised when your program puts a Lamborghini ahead of a Prius ["8" > "48"].
1

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))

Comments

0

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))

Comments

-1
Public Class Form1
    Public Structure EstruturaPessoa
        Dim nome As String
        Dim idade As Integer
    End Structure

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim nome(,) As String = {{"Milton Inácio Pozza", 30}, _
                                 {"Araci Moraes", 34}, _
                                 {"Marli Lipi Jesus", 21}, _
                                 {"Gerson Guebur", 45}, _
                                 {"Marli Ulths", 72}, _
                                 {"Mauro Jesus Nadolni", 56}, _
                                 {"Cristiano Kobashikawa Ferreira", 44}, _
                                 {"Débora Nadolni", 90}, _
                                 {"Samanta Gomes Guebur", 66}, _
                                 {"Miguel Barbosa", 42}, _
                                 {"Luis Jesus", 24} _
                                }
        Dim Pessoa As EstruturaPessoa
        Dim ListaPessoa = New List(Of EstruturaPessoa)
        Dim i As Integer

        'Load ListaPessoa
        For i = 0 To (nome.Length / 2) - 1
            Pessoa.nome = nome(i, 0)
            Pessoa.idade = nome(i, 1)
            ListaPessoa.Add(Pessoa)
        Next i

        'Fill the ListView1 with the ListaPessoa before sorting
        For Each item_pessoa In ListaPessoa
            With Me.ListView1
                .Items.Add(item_pessoa.nome)
                With .Items(.Items.Count - 1).SubItems
                    .Add(item_pessoa.idade)
                End With
            End With
        Next

        'Sort ListaPessoa
        ListaPessoa.Sort(Function(c1, c2) c1.nome.CompareTo(c2.nome))

        'Modifiy the 6th item of ListaPessoa
        Pessoa = ListaPessoa(5)
        Pessoa.nome += " ***"   'Acrescenta asteriscos ao nome
        Pessoa.idade = 99       'Modifica a idade para 99
        ListaPessoa(5) = Pessoa 'Atualiza o item na ListaPessoa

        'Fill ListView2 with the ListaPessoa after sorting
        For Each item_pessoa In ListaPessoa
            With Me.ListView2
                .Items.Add(item_pessoa.nome)
                With .Items(.Items.Count - 1).SubItems
                    .Add(item_pessoa.idade)
                End With
            End With
        Next
    End Sub
End Class

1 Comment

Welcome to Stack Overflow! Stack Overflow generally works in English; please make sure your post is as useful as possible to others by translating the comments, and possibly the variable names as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.