How can I sort my processes by name?

This question already has an answer here:

  • How to Sort a List<T> by a property in the object 19 answers

  • LINQ makes it really easy to work with collections of things and it has an OrderBy option. Once you have them ordered you can again use LINQ to Select the process name of each and then use those names and use Select to insert them into the list.

    proc = Process.GetProcesses()
        .OrderBy(p => p.ProcessName)
        .ToArray();
    proc.Select(p => p.ProcessName)
        .Select(listBox1.Items.Add);
    

    请检查这一点,我修改了你的功能:

    void GetAllProcess()
    {
        proc = Process.GetProcesses();
        listBox1.Items.Clear();
        foreach (Process p in proc.OrderBy(m=>m.ProcessName))
        {
            listBox1.Items.Add(p.ProcessName);
        }
    }
    
    链接地址: http://www.djcxy.com/p/70952.html

    上一篇: 按第二个值(int)对List <string []>进行排序

    下一篇: 我怎样才能按名称排序我的流程?