Link to home
Start Free TrialLog in
Avatar of vivekj2004
vivekj2004

asked on

C# array, .Net, dot net

I have an array of user defined type. one of the elements in the array object is long, and I want to sort the array by that element, and pick the array object with the smallest number in that element. How can I do this. Can somebody write the code for me? Will there be a difference if I want this for the integer intead of long

A[] ->consists of ->(string s, integer i, long l)
Avatar of kris_per
kris_per


Something like this...?

Object[] array = new Object[3];
            array[0] = "2582345"; // string
            array[1] = 54321;   // int
            array[2] = long.MaxValue;

            Object[] sortedArray = array.OrderBy(o => Convert.ToDouble(o)).ToArray();

            // now first element is the smallest
            object smallest = sortedArray[0];

Open in new window


List<MyType> array =new List<MyType>();
...
array.Sort((MyType obj1, MyType obj2) => obj1.L.CompareTo(obj2.L));

OR do you want something like this with user defined type:
public class Customer
        {
            public string ID;
        }

        [STAThread]
        static void Main()
        {
            Customer[] array = new Customer[3];
            array[0] = new Customer { ID = "2582345" };
            array[1] = new Customer { ID = "54321" };
            array[2] = new Customer { ID = "123123123" };

            Customer[] sortedArray = array.OrderBy(c => Convert.ToDouble(c.ID)).ToArray();

            // now first element is the smallest
            Customer smallestIDCustomer = sortedArray[0];

Open in new window

Give a preview of you existing code. Little confusing whether it's an hybrid array or collection of user type contain string, long, decimal or something like that.
ASKER CERTIFIED SOLUTION
Avatar of fromer
fromer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vivekj2004

ASKER

This was .Net 2.0 code. Traditional code, that's why I chose this as complete solution.