我正在为集合中的搜索实现线性搜索然后我想为什么不使用二进制搜索,为此我必须使用排序。 虽然我可以实现它们,但我想知道它们在 .net 本身中的位置。我希望它会出现在 .Net 中。
最佳答案
如果我没记错的话,这就是你要找的东西
public class Person
{
public int age;
public string name;
public Person(int age, string name)
{
this.age = age;
this.name = name;
}
}
List<Person> people = new List<Person>();
people.Add(new Person(50, "Fred"));
people.Add(new Person(30, "John"));
people.Add(new Person(26, "Andrew"));
people.Add(new Person(24, "Xavier"));
people.Add(new Person(5, "Mark"));
people.Add(new Person(6, "Cameron"));
用于搜索
// with delegate
List<Person> young = people.FindAll(delegate(Person p) { return p.age < 25; });
// with lammda
List<Person> young = people.FindAll(a=> a.age < 25);
用于排序
// with delegate
people.Sort(delegate(TestKlasse a, TestKlasse b) { return a.age.CompareTo(b.age); });
// with lambda function
people.Sort((a, b) => a.age.CompareTo(b.age));
关于c# - .net有排序和搜索功能吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3130664/