c#对列表进行排序<;KeyValuePair<;int,string>&燃气轮机;

c#对列表进行排序<;KeyValuePair<;int,string>&燃气轮机;,c#,list,sorting,compare,keyvaluepair,C#,List,Sorting,Compare,Keyvaluepair,在C#中,我想根据列表中每个字符串的长度对列表进行排序。在Psuedo Java中,这将是匿名的,看起来像: Collections.Sort(someList, new Comparator<KeyValuePair<int, string>>( { public int compare(KeyValuePair<int, string> s1, KeyValuePair<int, string> s2) {

在C#中,我想根据列表中每个字符串的长度对
列表进行排序。在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
      }
    });
Collections.Sort(someList,新比较器({
公共整数比较(键值对s1、键值对s2)
{
return(s1.Value.Length>s2.Value.Length)?1:0;//在此处指定我的排序条件
}
});
  • 如何获得上述功能
  • 您可以使用linq呼叫

    有关@Guffa指出的查找内容的更多信息,基本上它只会在需要时执行
    这将使要执行的表达式返回一个列表。

    在C#中的等效方法是使用lambda表达式和排序方法:

    someList.Sort((x, y) => x.Value.Length.CompareTo(y.Value.Length));
    
    您还可以使用
    OrderBy
    扩展方法。它的代码稍微少一些,但由于创建列表副本而不是对其进行排序,因此增加了更多的开销:

    someList = someList.OrderBy(x => x.Value.Length).ToList();
    
    你可以用这个

    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);
        }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    班级计划
    {
    静态整数比较1(KeyValuePair a、KeyValuePair b)
    {
    返回a.Key.CompareTo(b.Key);
    }
    静态整数比较2(KeyValuePair a、KeyValuePair b)
    {
    返回a.Value.CompareTo(b.Value);
    }
    静态void Main()
    {
    var list=新列表();
    添加(新的KeyValuePair(“Perl”,7));
    添加(新的KeyValuePair(“Net”,9));
    添加(新的KeyValuePair(“点”,8));
    //使用Compare1作为比较委托。
    列表。排序(比较1);
    foreach(列表中的变量对)
    {
    控制台写入线(对);
    }
    Console.WriteLine();
    //使用Compare2作为比较委托。
    list.Sort(Compare2);
    foreach(列表中的变量对)
    {
    控制台写入线(对);
    }
    }
    }
    
    请注意,代码实际上没有做任何工作。您必须使用
    ToList
    来实际执行排序,并将其分配回变量。
    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);
        }
        }
    }