-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestList.cs
105 lines (92 loc) · 3.19 KB
/
TestList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
// Simple business object. A PartId is used to identify the type of part
// but the part name can change.
public class Part
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " + PartId + " Name: " + PartName;
}
/*public override bool Equals(object obj)
{
if (obj == null) return false;
Part objAsPart = obj as Part;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public int SortByNameAscending(string name1, string name2)
{
return name1.CompareTo(name2);
}
*/
// Default comparer for Part type.
public int CompareTo(Part comparePart)
{
// A null value means that this object is greater.
if (comparePart == null)
return 1;
else
return this.PartId.CompareTo(comparePart.PartId);
}
/* public override int GetHashCode()
{
return PartId;
}
public bool Equals(Part other)
{
if (other == null) return false;
return (this.PartId.Equals(other.PartId));
}
// Should also override == and != operators.
*/
}
public class Example
{
public static void Main()
{
// Create a list of parts.
List<Part> parts = new List<Part>();
// Add parts to the list.
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName= "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ;
// Name intentionally left null.
parts.Add(new Part() { PartId = 1334 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
// Write out the parts in the list. This will call the overridden
// ToString method in the Part class.
Console.WriteLine("\nBefore sort:");
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// Call Sort on the list. This will use the
// default comparer, which is the Compare method
// implemented on Part.
parts.Sort();
Console.WriteLine("\nAfter sort by part number:");
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// This shows calling the Sort(Comparison(T) overload using
// an anonymous method for the Comparison delegate.
// This method treats null as the lesser of two values.
/*parts.Sort(delegate(Part x, Part y)
{
if (x.PartName == null && y.PartName == null) return 0;
else if (x.PartName == null) return -1;
else if (y.PartName == null) return 1;
else return x.PartName.CompareTo(y.PartName);
});
Console.WriteLine("\nAfter sort by name:");
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}*/
}
}