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,

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much, that was all info I needed!
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?
@Sliver2009: You can put an if statement, i.e. If result of CompareTo equals to 0, make another CompareTo, and return that.
@Sliver2009: I modified my example, please check it out. Reference: CompareTo @ MSDN
Thanks, I am not an expert in VB.Net, but I get the rationale behind comparing now!
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))

5 Comments

I saw that topic to, but I do not get the following: Function(x, y) x.Lang.CompareTo(y.Lang)
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.
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.
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?)
Grim, even with .NET 2.0 you can specify a function as an argument for Sort, you just need it predefined.

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.