C# 0 The Complete Reference


Using a Non-Generic IComparer



Download 4,07 Mb.
Pdf ko'rish
bet1000/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   996   997   998   999   1000   1001   1002   1003   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Using a Non-Generic IComparer

The non-generic 



IComparer

 defines only one method, 



Compare( )

, which is shown here:

int Compare(object 

obj1

, object 



obj2

)

Compare( )

 compares 

obj1

 to 


obj2

. To sort in ascending order, your implementation must 

return zero if the objects are equal, a positive value if 

obj1

 is greater than 



obj2,

 and a negative 

value if 

obj1

 is less than 



obj2.

 You can sort in descending order by reversing the outcome of 

the comparison. The method can throw an 

ArgumentException

 if the type of 



obj

 is not 


compatible for comparison with the invoking object.

An

IComparer

 can be specified when constructing a 

SortedList

, when calling 



ArrayList.Sort(IComparer)

, and at various other places throughout the collection classes. 

The main advantage of using 

IComparer

 is that you can sort objects of classes that do not 

implement

IComparable

.

The following program reworks the non-generic inventory program so that it uses an 



IComparer

 to sort the inventory list. It first creates a class called 



CompInv

 that implements 



IComparer

 and compares two 



Inventory

 objects. An object of this class is then used in a call 

to

Sort( )

 to sort the inventory list.

// Use IComparer.

using System;

using System.Collections;

// Create an IComparer for Inventory objects.

class CompInv : IComparer {

  // Implement the IComparer interface.

  public int Compare(object obj1, object obj2) {

    Inventory a, b;

    a = (Inventory) obj1;

    b = (Inventory) obj2;

    return a.name.CompareTo(b.name);

  }


}

www.freepdf-books.com




806

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

class Inventory {

  public string name;

  double cost;

  int onhand;

  public Inventory(string n, double c, int h) {

    name = n;

    cost = c;

    onhand = h;

  }


  public override string ToString() {

    return

      String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}",

                    name, cost, onhand);

  }

}

class IComparerDemo {



  static void Main() {

    CompInv comp = new CompInv();

    ArrayList inv = new ArrayList();

    // Add elements to the list.

    inv.Add(new Inventory("Pliers", 5.95, 3));

    inv.Add(new Inventory("Wrenches", 8.29, 2));

    inv.Add(new Inventory("Hammers", 3.50, 4));

    inv.Add(new Inventory("Drills", 19.88, 8));

    Console.WriteLine("Inventory list before sorting:");

    foreach(Inventory i in inv) {

      Console.WriteLine("   " + i);

    }


    Console.WriteLine();

    // Sort the list using an IComparer.

    inv.Sort(comp);

    Console.WriteLine("Inventory list after sorting:");

    foreach(Inventory i in inv) {

      Console.WriteLine("   " + i);

    }

  }


}

The output is the same as the previous version of the program.



Using a Generic IComparer

The


IComparer

 interface is the generic version of 



IComparer

. It defines the generic 

version of 

Compare( )

, shown here:

int Compare(T 

obj1

, T 


obj2

)

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   996   997   998   999   1000   1001   1002   1003   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish