c# 排序列表<KeyValuePair<int, string>>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14544953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:13:20  来源:igfitidea点击:

c# Sorting a List<KeyValuePair<int, string>>

c#listsortingcomparekeyvaluepair

提问by CodeKingPlusPlus

In C# I would like to sort a List<KeyValuePair<int, string>>by the length of each string in the list. In Psuedo-Java this would be an anonymous and would look something like:

在 C# 中,我想List<KeyValuePair<int, string>>按列表中每个字符串的长度对 a 进行排序。在 Psuedo-Java 中,这将是一个匿名的,看起来像:

  Collections.Sort(someList, new Comparator<KeyValuePair<int, string>>( {
      public int compare(KeyValuePair<int, string> s1, KeyValuePair<int, string> s2)
      {
          return (s1.Value.Length > s2.Value.Length) ? 1 : 0;    //specify my sorting criteria here
      }
    });
  1. How do I get the above functionality?
  1. 如何获得上述功能?

采纳答案by Guffa

The equivalent in C# would be to use a lambda expression and the Sortmethod:

C# 中的等价物是使用 lambda 表达式和Sort方法:

someList.Sort((x, y) => x.Value.Length.CompareTo(y.Value.Length));

You can also use the OrderByextension method. It's slightly less code, but it adds more overhead as it creates a copy of the list instead of sorting it in place:

您也可以使用OrderBy扩展方法。它的代码略少,但它增加了更多的开销,因为它创建了列表的副本而不是就地排序:

someList = someList.OrderBy(x => x.Value.Length).ToList();

回答by BrunoLM

You can use linq calling OrderBy

您可以使用 linq 调用OrderBy

list.OrderBy(o => o.Value.Length);

For more info on what @Guffa pointed out look for Linq and Deferred Execution, basically it will only execute when needed. So to immediately return a list from this line you need to add a .ToList()which will make the expression to be executed returning a list.

有关@Guffa 指出的更多信息,请查找Linq 和 Deferred Execution,基本上它只会在需要时执行。因此,要立即从这一行返回一个列表,您需要添加一个.ToList(),这将使要执行的表达式返回一个列表。

回答by Guffa

u can use this

你可以用这个

using System;
using System.Collections.Generic;

class Program
{
    static int Compare1(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
    {
    return a.Key.CompareTo(b.Key);
    }

    static int Compare2(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
    {
    return a.Value.CompareTo(b.Value);
    }

    static void Main()
    {
    var list = new List<KeyValuePair<string, int>>();
    list.Add(new KeyValuePair<string, int>("Perl", 7));
    list.Add(new KeyValuePair<string, int>("Net", 9));
    list.Add(new KeyValuePair<string, int>("Dot", 8));

    // Use Compare1 as comparison delegate.
    list.Sort(Compare1);

    foreach (var pair in list)
    {
        Console.WriteLine(pair);
    }
    Console.WriteLine();

    // Use Compare2 as comparison delegate.
    list.Sort(Compare2);

    foreach (var pair in list)
    {
        Console.WriteLine(pair);
    }
    }
}