using System;
using System.Collections;
public class Temperature : IComparable
{
// The temperature value
protected string temperatureF;
public int CompareTo(object obj)
{
if (obj == null) return 1;
if (obj!= null)
return (((string)obj).temperatureF).CompareTo(this.temperatureF);
else
throw new ArgumentException(“Object is not a Temperature”);
}
public string Fahrenheit
{
get
{
return this.temperatureF;
}
set
{
this.temperatureF = value;
}
}
}
public class CompareTemperatures
{
public static void Main()
{
ArrayList temperatures = new ArrayList();
// Initialize random number generator.
Random rnd = new Random();
// Generate 10 temperatures between 0 and 100 randomly.
Temperature a = new Temperature();
a.Fahrenheit = “Oded”;
temperatures.Add(a);
Temperature b = new Temperature();
a.Fahrenheit = “edf”;
temperatures.Add(b);
Temperature c = new Temperature();
c.Fahrenheit = “afjg”;
temperatures.Add(c);
Temperature d = new Temperature();
d.Fahrenheit = “Aog”;
temperatures.Add(d);
// Sort ArrayList.
temperatures.Sort();
foreach (Temperature temp in temperatures)
Console.WriteLine(temp.Fahrenheit);
}
}
20
ArrayList 只提供了
20
// Generate 10 temperatures between 0 and 100 randomly.
Temperature a = new Temperature();
a.Fahrenheit = “Oded”;
temperatures.Add(a);
Temperature b = new Temperature();
a.Fahrenheit = “edf”;
temperatures.Add(b);
Temperature c = new Temperature();
c.Fahrenheit = “afjg”;