Implements IComparer to create Reversed Comparer : IComparer « Collections « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Collections » IComparer 
8.21.3.Implements IComparer to create Reversed Comparer
Imports System
Imports System.Collections.Generic

Public Class ReverseComparer
    Implements IComparer(Of String)
    Public Function Compare(ByVal x As String, _
        ByVal y As StringAs Integer _
        Implements IComparer(Of String).Compare
        Return y.CompareTo(x)

    End Function
End Class

Public Class Example
    Public Shared Sub Main()

        Dim employees() As String = {"A","B","E","C","F","G"  }

        Dim employeeSizes() As Integer = 405322118 }

        For i As Integer = To employees.Length - 1
            Console.WriteLine("{0}: up to {1} meters long.", employees(i), employeeSizes(i))
        Next

        Console.WriteLine("Sort(employees, employeeSizes)")
        Array.Sort(employees, employeeSizes)

        For i As Integer = To employees.Length - 1
            Console.WriteLine("{0}: up to {1} meters long.", _
                employees(i), employeeSizes(i))
        Next

        Dim rc As New ReverseComparer()

        Console.WriteLine("Sort(employees, employeeSizes, rc)")
        Array.Sort(employees, employeeSizes, rc)

        Console.WriteLine()
        For i As Integer = To employees.Length - 1
            Console.WriteLine("{0}: up to {1} meters long.",employees(i), employeeSizes(i))
        Next

        Console.WriteLine("Sort(employees, employeeSizes, 3, 3)")
        Array.Sort(employees, employeeSizes, 33)

        Console.WriteLine()
        For i As Integer = To employees.Length - 1
            Console.WriteLine("{0}: up to {1} meters long.",employees(i), employeeSizes(i))
        Next

        Console.WriteLine("Sort(employees, employeeSizes, 3, 3, rc)")
        Array.Sort(employees, employeeSizes, 33, rc)

        Console.WriteLine()
        For i As Integer = To employees.Length - 1
            Console.WriteLine("{0}: up to {1} meters long.",employees(i), employeeSizes(i))
        Next


    End Sub

End Class
8.21.IComparer
8.21.1.Implements IComparer
8.21.2.Use custom Comparer to compare objects
8.21.3.Implements IComparer to create Reversed Comparer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.