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

标签 c# .net sorting collections search

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

相关文章:

c# - 如何获得在集合数量中找到的最长匹配项,顺序很重要

c# - 如何确保我不会丢失来自 TCP 的数据?

.net - ASP.NET - 从内容页访问母版页元素

c# - Silverlight WCF 抛出远程服务器返回错误 : NotFound

php - 根据条件对数组进行排序

c# - Entity Framework 5 : Entity is lazy loaded but the navigation properties are null when observing the Local collection

c# - 将 MBF 单精度和 double 转换为 IEEE

.net - 未知模块中发生 'System.TypeLoadException' 类型的未处理异常

c++ - <algorithm> 中的 "sort"函数可以用于对二维字符数组进行排序吗?

python - 当 Python 列表中有多余字符时进行匹配