vb.net list reorder on keyword preference
Account Home | Help | Blog | Contact us | Log Out

Welcome to Kbytes > Articles

vb.net list reorder on keyword preference

Posted By: siteadmin on 09/07/2015 11:23:00

We had a project where a list of strings needed to be sorted on with some keywords (if they appeared in the list) to come up first.  

If they didn't appear then, no problem, the list should be returned entirely.

This seems like a job for a lambda function.  There are some examples for e.g. length searching but no exact function.

This is what we found.  Maybe it from javascript experience by we expected the function to return 0 or 1.  Investigating and putting a break point on the 'c' variable below shows it is actually -1 and +1 that are returned and give the sort.

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim l As List(Of String) = New List(Of String)
        Dim f = Function(elementA As String, elementB As String)
                    If elementA.Contains("danube") Then
                        Return -1
                    End If
                    Return 1
                End Function
        Dim s = Function(a As String, b As String)
                    Dim c = a.Length.CompareTo(b.Length)
                    Return c                                              '<--- breakpoint here shows -1 or +1
                End Function
        ' Sort using lambda expression.
        For Each element As String In l
        For Each element As String In l
    End Sub

blog comments powered by Disqus

Kbytes Home | Privacy Policy | Contact us | Testing Area

© 2004 - 2024 1 Oak Hill Grove Surbiton Surrey KT6 6DS Phone: +44(020) 8123 1321