我有這個練習,儘管我嘗試實現IComparable接口,我無法按重量從最少到最多排序列表............... .................................................. ...........列表沒有被排序
using System;
using System.Collections.Generic;
namespace Program
{
abstract class Animal : IComparable<Animal>
{
private double weight;
private int name;
abstract public override string ToString();
public int CompareTo(Animal right)
{
return weight.CompareTo(right.weight);
}
}
class Cat : Animal
{
public Cat(double weight, string name)
{
this.weight = weight;
this.name = name;
}
private double weight;
private string name;
public override string ToString()
{
return "I'm the cat " + name + " and I weight " + weight;
}
}
class Hello
{
static void Main(string[] args)
{
List<Animal> myArray = new List<Animal>();
for (int counter = 9; counter > 0; counter--)
{
myArray.Add(new Cat(counter * 3.5, counter.ToString()));
}
foreach (Animal CatOrDog in myArray)
{
Console.WriteLine(CatOrDog.ToString());
}
myArray.Sort();
foreach (Animal CatOrDog in myArray)
{
Console.WriteLine(CatOrDog.ToString());
}
}
}
}
我認爲你可以嘗試使用'LINQ'' OrderBy'來代替排序來給出排序的一些條件 – Ian
問題是這是一個關於List和IComparable接口的練習。 – user3646717
我看...練習.. – Ian