2

I have a question concerning sorting lists of classes in VB.Net. It seems every subject which is discussing this kind of sorting is not really clear for me.

I have a class Language with the following variables: - Lang as a string - Knowledge as a integer

I have got a list containing a couple of language classes in it. How can I sort on the Lang variable (Alphabetically sort the language classes in the list)?

Greetings,

Revils
  • 1,478
  • 1
  • 14
  • 31

2 Answers2

2

This was answered in a previous StackOverflow question: Sort a List of Object in VB.NET

Use Sort along with a custom function to compare the Lang variable.

theList.Sort(Function(x, y) x.Lang.CompareTo(y.Lang))
Community
  • 1
  • 1
James Hunt
  • 2,448
  • 11
  • 23
  • I saw that topic to, but I do not get the following: Function(x, y) x.Lang.CompareTo(y.Lang) – Revils Jun 23 '14 at 13:29
  • Which version of the .NET Framework are you using? Older versions do not support these functions; you need LINQ and lambda functionality to do it. – Grim Jun 23 '14 at 13:31
  • Sort accepts a function as an argument. This function is how two elements are compared to decide how one element is put infront of another. The function written in that code compares the 2 elements by their Lang variable, ordering them via it. – James Hunt Jun 23 '14 at 13:32
  • So something like: Private Sub Counter(Language x, Language y) if x > y then ..? End Sub list.Sort(Counter xLang.CompareTo(y.Lang) And I do not understand how a function should be? (It should return two values? Both x and y?) – Revils Jun 23 '14 at 13:33
  • Grim, even with .NET 2.0 you can specify a function as an argument for Sort, you just need it predefined. – James Hunt Jun 23 '14 at 13:38
2

Implement IComparable on your class, then use Sort:

Private Class Language : Implements IComparable(Of Language)
  Public Property Lang As String
  Public Property Knowledge As Integer

  Sub New(lang As String)
    Me.Lang = lang
  End Sub

  Public Function CompareTo(other As Language) As Integer _
                                    Implements IComparable(Of Language).CompareTo
    Dim comp As Integer = Me.Lang.CompareTo(other.Lang)
    'If comp = 0 Then Return Me.Knowledge.CompareTo(other.Knowledge)
    Return comp
  End Function
End Class

Sub Main()
  Dim lst As New List(Of Language)
  lst.Add(New Language("fr"))
  lst.Add(New Language("en"))
  lst.Add(New Language("de"))
  lst.Sort()
End Sub

EDIT: Added a hint on how to sort by multiple properties.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • 1
    Thank you very much, that was all info I needed! – Revils Jun 23 '14 at 13:51
  • Is it also possible to sort/compare on two variables? Say I first sort on Lang first. if I have two Lang, than sort on the knowledge level? – Revils Jun 24 '14 at 09:34
  • 1
    @Sliver2009: You can put an if statement, i.e. If result of `CompareTo` equals to 0, make another `CompareTo`, and return that. – Victor Zakharov Jun 24 '14 at 13:25
  • 1
    @Sliver2009: I modified my example, please check it out. Reference: [CompareTo @ MSDN](http://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx) – Victor Zakharov Jun 24 '14 at 13:30
  • 1
    Thanks, I am not an expert in VB.Net, but I get the rationale behind comparing now! – Revils Jun 24 '14 at 13:47