Saturday, 2 February 2013

String Sort using LINQ orderby VB.NET


String Sort using LINQ VB.NET

            
Sorting a string array using LINQ. suppose LINQ array has firstname,lastname ,default sort works on firstname. If user wants to sort on LastName. then you can follow this logic.
 name contains format:  FirstName,LastName
  Dim strFirstLastNames() As String =  New String() {"Amar,josuva","peter,hans","tom,cruise","Preeti,Zinta"};

Private  Sub SortByFirstName()
            var query = from name in strFirstLastNames
                        orderby name
                        Dim name As select

            Dim str As String
            For Each str In query
                Console.WriteLine(str)
            Next
End Sub

OUTPUT(alphabetical order)
--------------------

Amar,josuva
peter,hans
Preeti,Zinta
tom,cruise


Private  Sub SortByLastName()
            Console.WriteLine()

            var query = from name in strFirstLastNames
                        Dim lastname As let =  name.Split(New Char()
                        {
                            ","c
                        }
)(1)
                        orderby lastname
                        Dim name As select
               'For Descending  orderby lastname descending
            Dim str As String
            For Each str In query
                Console.WriteLine(str)
            Next

  End Sub
 
 OUTPUT(last name, cruise, hans ,josuva,Zinta in sorted order).
----------------------

tom,cruise
peter,hans
Amar,josuva
Preeti,Zinta


Tags:  String Sort using LINQ orderby VB.NET,LINQ LET CLAUSE,LINQ ORDERBY clause,LINQ ORDER BY keyword, String Sort using LINQ, String split in LET clause, String sorting in descending order, Order Strings in VB.NET,Arrange Strings in Ascending/descending order.