shipping boxes sizes find largest side and smallest size

I am doing some shipping calculations. I need some help trying to figure this out. Basically, I have a generic list of products with Length, Width, and Height properties.

I would like to EASILY look at the products and find the largest values of all three properties. From here, I can do some math and figure out the box size based on the # of products.

My initial thought would be to make 3 arrays and find the max of each. Just wanted to see if there was a simpler or cooler way I didnt' know.

Thanks!

Answers


Sounds like an array of arrays. As you read each element (box) from your data source (SQL Server, XML, etc), create an 3-member array and insert the attributes in order of size. Then, add the three-member array to an array of arrays. You can then sort the array of arrays by the first, second or third member using LINQ or some other function.

Box1,2,2,3
Box2,5,10,1
Box3,8,4,7

Becomes:

{  {10,5,1},  {8,7,4},  {3,2,2}  } // First

or

{  {8,7,4},  {10,5,1},  {3,2,2}  } // Second

or

{  {8,7,4},  {3,2,2},  {10,5,1}  } // Third

Then, you can sort the array by the first element, second element, etc.

You could easily build an array of arrays in a single statement using LINQ, but exactly how you would do so depends on the data source. Assuming you have an class named Box with three parameters, Length, Width and Height, and that you have created a strongly-typed collection containing instances of this class:

class BoxSorter {

    public IEnumerable<Box> Boxes {
        get;
        private set;
    }

    class Box {
        public double Height {
            get;
            set;
        }

        public double Width {
            get;
            set;
        }

        public double Length {
            get;
            set;
        }
    }

    public void Initialize() {

        this.Boxes = new List<Box>( new Box[] { 
            new Box() { Height = 2, Length = 2, Width = 3 },
            new Box() { Height = 5, Length = 10, Width = 1 },
            new Box() { Height = 8, Length = 4, Width = 7 }
        } );

    }

    public void Sort() {

        var l_arrayOfArrays =
            this.Boxes.Select(
            // Create an array of the Height, Length and Width, then sort the array elements (largest to smallest)
                b => new double[] { b.Height, b.Length, b.Width }.OrderByDescending( v => v ).ToArray()
            );

        var l_dimension1 =
            l_arrayOfArrays.OrderByDescending(
            // Sort the array of arrays by the first (and largest) dimension
                a => a[0]
            );

        var l_dimension2 =
            l_arrayOfArrays.OrderByDescending(
            // Sort the array of arrays by the second (and middle) dimension
                a => a[1]
            );

        var l_dimension3 =
            l_arrayOfArrays.OrderByDescending(
            // Sort the array of arrays by the third (and smallest) dimension
                a => a[2]
            );

    }

}

Need Your Help

Pass GET variable from URL to .js, and from the .js to .php

php javascript jquery html ajax

Hi I currently have a JS file being called to populate my html page with dynamic data. My JS file calls a PHP file to fetch stuff from my sqldb and my PHP file echos json_encode the stuff it got fr...

regex to extract filename

java regex

I've got a plain text web response and need to extract the filename.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.