gpt4 book ai didi

c# - .net有排序和搜索功能吗

转载 作者:行者123 更新时间:2023-11-30 19:33:43 25 4
gpt4 key购买 nike

我正在为集合中的搜索实现线性搜索然后我想为什么不使用二进制搜索,为此我必须使用排序。虽然我可以实现它们,但我想知道它们在 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com