Θα μπορούσε να χρησιμοποιηθεί αντί «Collections», «Collections.Generic» που strong type το IComparer.
public class Document
{
string docId;
string name;
int numberWords;
double score;
public Document() {
}
public Document(string name, string docId, int numberWords) {
this.name = name;
this.docId = docId;
this.numberWords = numberWords;
score = 0;
}
public string DocId {
get { return docId; }
set { docId = value; }
}
public string Name {
get { return name; }
set { name = value; }
}
public int NumberWords {
get { return numberWords; }
set { numberWords = value; }
}
public double Score {
get { return score; }
set { score = value; }
}
public void setScoreZero() {
Score = 0;
}
public override string ToString() {
return string.Format(CultureInfo.CurrentCulture, "{0}\t{1:N}", Name, Score);
}
}
public class DocumentComparer : IComparer
{
#region IComparer Members
public int Compare(object x, object y) {
if (x == null) return y == null ? 0 : -1;
if (y == null) return 1;
if (((Document)x).Score > ((Document)y).Score) return 1;
if (((Document)x).Score < ((Document)y).Score) return -1;
return 0;
}
#endregion
}
static void Main() {
Random numberWordsRandom = new Random();
Random scoreRandom = new Random();
ArrayList documents = new ArrayList();
for (int i = 0; i < 20; i++) {
Document document = new Document("Book" + i.ToString(CultureInfo.InvariantCulture), Guid.NewGuid().ToString(), numberWordsRandom.Next(1500));
document.Score = scoreRandom.Next(100);
document.Score /= scoreRandom.Next(23) + 1;
documents.Add(document);
}
documents.Sort(new DocumentComparer());
foreach (Document document in documents)
Console.WriteLine(document);
}
while (!dead) learn();