Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过一个排序变量对分类对象的数据结构进行排序的最佳方法?_C#_Algorithm_Sorting - Fatal编程技术网

C# 通过一个排序变量对分类对象的数据结构进行排序的最佳方法?

C# 通过一个排序变量对分类对象的数据结构进行排序的最佳方法?,c#,algorithm,sorting,C#,Algorithm,Sorting,给定包含一些其他对象的对象列表/数组,我希望使用其中一个参数进行排序。例如,如果我有一个整数的多维数组,我想按它们的中点排序 我的方法是创建一个包含1的匿名对象。中点和2。每个条目包含2个整数的列表。然后登记这些对象,并使用反射按中点参数进行访问和排序 有没有更标准的方法来解决这个问题?这有点笨拙,因为我必须创建一个虚拟对象并清除匿名列表对象才能这样做 class Program { static void Main(string[] args) { int[,]

给定包含一些其他对象的对象列表/数组,我希望使用其中一个参数进行排序。例如,如果我有一个整数的多维数组,我想按它们的中点排序

我的方法是创建一个包含1的匿名对象。中点和2。每个条目包含2个整数的列表。然后登记这些对象,并使用反射按中点参数进行访问和排序

有没有更标准的方法来解决这个问题?这有点笨拙,因为我必须创建一个虚拟对象并清除匿名列表对象才能这样做

class Program
{
    static void Main(string[] args)
    {
        int[,] input = new int[,] { { 5, 6 }, { 0, 1 }, { 2, 20 } };

        var itemList = new []{ 
          new { sortVar = 0.1f, item = new List<int>() } }.ToList();

        itemList.Clear();

        for(int i=0; i<input.GetLength(0); i++)
        {
            List <int> temp = new List<int> (new int[] { input[i,0], input[i, 1] });

            var item = new { 
              sortVar = float.Parse((input[i,0]+input[i,1]).ToString())/2,
              item = temp };

            itemList.Add(item);
        }

        var itemList2 = itemList.OrderBy(x => x.sortVar);
    }
}
类程序
{
静态void Main(字符串[]参数)
{
int[,]input=newint[,]{{5,6},{0,1},{2,20};
var itemList=new[]{
新的{sortVar=0.1f,item=new List()}}.ToList();
itemList.Clear();
对于(int i=0;i x.sortVar);
}
}

您的方法的简短linq版本是直接在
OrderBy
子句中使用该方法(如果您倾向于使用二维列表):

我建议将2D数组更改为锯齿数组(数组的数组):

并提取标准(
中点
)作为方法

   private static double MidPoint(IEnumerable<int> value) {
      int[] sorted = value.OrderBy(item => item).ToArray(); 

      // / 2.0: we don't want integer division: (1 + 4) / 2 == 2 when (1 + 4) / 2.0 == 2.5
      return sorted.Length % 2 == 0
        ? (sorted[sorted.Length / 2 - 1] + sorted[sorted.Length / 2]) / 2.0
        : sorted[sorted.Length / 2];
    } 
如果要创建新的排序数组,请执行以下操作:

 // we'll have to arrays: initial (unsorted) input, and sorted sortedInput
 var sortedInput = input
   .OrderBy(line => MidPoint(line))
   .ToArray();

.中点不是
平均值
,而是排序数组中的中间项:
[1,2100]->2;[1,2,4100]->(2+4)/2==3
@DmitryBychenko看着OP的代码,他不是在计算中位数,而是在计算平均值:
float.Parse((输入[i,0]+input[i,1]).ToString())/2
当我尝试他的代码时,
sortVar
的值为
0.5
5.6
11
。这显然不是medianin病例数组有
1
2
项,中位数等于average@DmitryBychenko虽然这个计算对我来说仍然很卑鄙:)我在我的帖子中添加了一个中位数。谢谢你的强调。啊,好的,明白了。谢谢
   int[][] input = new int[][] { 
     new int[]{ 5,  6 }, 
     new int[]{ 0,  1 }, 
     new int[]{ 2, 20 } }; 
   private static double MidPoint(IEnumerable<int> value) {
      int[] sorted = value.OrderBy(item => item).ToArray(); 

      // / 2.0: we don't want integer division: (1 + 4) / 2 == 2 when (1 + 4) / 2.0 == 2.5
      return sorted.Length % 2 == 0
        ? (sorted[sorted.Length / 2 - 1] + sorted[sorted.Length / 2]) / 2.0
        : sorted[sorted.Length / 2];
    } 
 // input will be sorted 
 Array.Sort(input, (a, b) => MidPoint(a).CompareTo(MidPoint(b)));
 // we'll have to arrays: initial (unsorted) input, and sorted sortedInput
 var sortedInput = input
   .OrderBy(line => MidPoint(line))
   .ToArray();