C# 需要关于如何根据体积对列表进行排序的帮助吗

C# 需要关于如何根据体积对列表进行排序的帮助吗,c#,C#,在这里,创建了一个包含长度、高度、深度和体积的框列表。现在,我需要这些盒子按照体积按顺序排列。请给我一个想法,我如何可以排序的箱子只考虑体积。我使用的是vs2008,linq不在这里工作。那么还有其他解决办法吗 using (StreamReader sr = new StreamReader("c:/containervalues.txt")) while ((line = sr.ReadLine()) != null)

在这里,创建了一个包含长度、高度、深度和体积的框列表。现在,我需要这些盒子按照体积按顺序排列。请给我一个想法,我如何可以排序的箱子只考虑体积。我使用的是vs2008,linq不在这里工作。那么还有其他解决办法吗

  using (StreamReader sr = new StreamReader("c:/containervalues.txt"))

                            while ((line = sr.ReadLine()) != null)
                            {
                                // create new instance of container for each line in file
                                Box box = new Box();
                                List<Box> listofboxes = new List<Box>();
                                string[] Parts = line.Split(' ');
                                // set non-static properties of container
                                box.bno = Parts[0];
                                box.length = Convert.ToDouble(Parts[1]);
                                box.height = Convert.ToDouble(Parts[2]);
                                box.depth = Convert.ToDouble(Parts[3]);
                                box.volume = Convert.ToDouble(Parts[4]);
                                // add container to list of containers
                                listofboxes.Add(box);

                            }
使用(StreamReader sr=newstreamreader(“c:/containervalues.txt”))
而((line=sr.ReadLine())!=null)
{
//为文件中的每一行创建容器的新实例
Box=新的Box();
List LISTOFBOKS=新列表();
字符串[]部分=行分割(“”);
//设置容器的非静态属性
box.bno=零件[0];
box.length=Convert.ToDouble(部分[1]);
box.height=Convert.ToDouble(部分[2]);
box.depth=Convert.ToDouble(第[3]部分);
box.volume=Convert.ToDouble(第[4]部分);
//将容器添加到容器列表中
列表框。添加(框);
}
试试这个:

您将需要使用Linq

listOfBoxes = listOfBoxes.OrderBy(x => x.volume).ToList();
试试这个:

您将需要使用Linq

listOfBoxes = listOfBoxes.OrderBy(x => x.volume).ToList();
List-sortedList=listofBox.OrderBy(x=>x.volume);
List sortedList=listofBox.OrderBy(x=>x.volume);

。。或者你可以降序订购

listOfBoxes = listOfBoxes.OrderByDescending(p => p.volume).ToList();

。。或者你可以降序订购

listOfBoxes = listOfBoxes.OrderByDescending(p => p.volume).ToList();

您可以使用委托作为泛型列表排序方法的参数:

using System;
using System.Collections.Generic;

namespace sortVolumen
{
class Program
{
    static void Main(string[] args)
    {
        List<box> BoxList = new List<box> { 
            new box { Width = 2, Height = 2, Depth = 2},
            new box { Width = 3, Height = 3, Depth = 3},
            new box { Width = 1, Height = 1, Depth = 1},
        };
        foreach (box myBox in BoxList)
        {
            Console.WriteLine(myBox.Volumen);
        }
        BoxList.Sort(delegate(box a, box b) { return a.Volumen < b.Volumen ? -1 : 1;});
            Console.WriteLine("after comparing");
            foreach (box myBox in BoxList)
            {
                Console.WriteLine(myBox.Volumen);
            }
            Console.ReadLine();
        }
    }
    class box
    {
        public double Width { get; set; }
        public double Height { get; set; }
        public double Depth { get; set; }
        public double Volumen {
            get { return Width * Height * Depth; }
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间排序卷
{
班级计划
{
静态void Main(字符串[]参数)
{
List BoxList=新列表{
新框{宽度=2,高度=2,深度=2},
新框{宽度=3,高度=3,深度=3},
新框{宽度=1,高度=1,深度=1},
};
foreach(框列表中的框myBox)
{
Console.WriteLine(myBox.Volumen);
}
排序(委托(框a,框b){返回a.Volumen

您还可以检查实现相同行为的方法。

您可以使用委托作为通用列表排序方法的参数:

using System;
using System.Collections.Generic;

namespace sortVolumen
{
class Program
{
    static void Main(string[] args)
    {
        List<box> BoxList = new List<box> { 
            new box { Width = 2, Height = 2, Depth = 2},
            new box { Width = 3, Height = 3, Depth = 3},
            new box { Width = 1, Height = 1, Depth = 1},
        };
        foreach (box myBox in BoxList)
        {
            Console.WriteLine(myBox.Volumen);
        }
        BoxList.Sort(delegate(box a, box b) { return a.Volumen < b.Volumen ? -1 : 1;});
            Console.WriteLine("after comparing");
            foreach (box myBox in BoxList)
            {
                Console.WriteLine(myBox.Volumen);
            }
            Console.ReadLine();
        }
    }
    class box
    {
        public double Width { get; set; }
        public double Height { get; set; }
        public double Depth { get; set; }
        public double Volumen {
            get { return Width * Height * Depth; }
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间排序卷
{
班级计划
{
静态void Main(字符串[]参数)
{
List BoxList=新列表{
新框{宽度=2,高度=2,深度=2},
新框{宽度=3,高度=3,深度=3},
新框{宽度=1,高度=1,深度=1},
};
foreach(框列表中的框myBox)
{
Console.WriteLine(myBox.Volumen);
}
排序(委托(框a,框b){返回a.Volumen

您还可以检查实现相同行为的方法。

我正在使用vs2008。这里linq不工作了。除了这个,还有别的解决办法吗?嗯,也许你可以用自己的排序方法。只检查音量。类似于insertionSort的东西,但由您使用盒子制作;)我正在使用vs2008。这里linq不工作了。除了这个,还有别的解决办法吗?嗯,也许你可以用自己的排序方法。只检查音量。类似于insertionSort的东西,但由您使用盒子制作;)