ARTICLE

Sorting in Arrays using VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 13, 2010
In this article I will explain you about Sorting in Arrays using VB.NET.
Download Files:
 
Reader Level:

As my previous article explain Reversing, and Searching of array. This article is all about the sophisticated example of using the IComparer interface and Sort function together. The IComparer interface allows you to define a Compare method in order to do a comparison between two elements of your array. This Compare method is called repeatedly by the Sort function in order to sort the array. The example given below defines a Compare method that does a comparison between two strings.

Example of Array Sorting

    ' sort an array according to the Nth element
    Imports System.Collections
        Module Module1
            Public Class CompareX
            Implements IComparer
            Private compareFrom As Integer = 0
            Public Sub New(ByVal i As Integer)
                compareFrom = i
            End Sub

            Public
Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
                Return [String].Compare(a.ToString().Substring(compareFrom), b.ToString().Substring(compareFrom))
            End Function
            End
Class

            Public
Class ArrListEx
                Shared Sub Main(ByVal args As String())
                    Dim arr As New ArrayList()
                arr.Add(
"aaaa9999")
                arr.Add(
"bbbb8888")
                arr.Add(
"cccc7777")
                arr.Add(
"dddd6666")
                arr.Sort(
New CompareX(4))
                Dim arrList As IEnumerator = arr.GetEnumerator()
                While arrList.MoveNext()
                    Console.WriteLine("Item: {0}", arrList.Current)
                End While
                    Console.ReadLine()
                End Sub
            End
Class
        End
Module

The program in above example, has this output

saarray.gif

Conclusion

Hope this article would have helped you in understanding how we Using IComparer interface and sort function together in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor