Solved

C# array, .Net, dot net

Posted on 2010-09-21
6
479 Views
Last Modified: 2012-06-21
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)
0
Comment
Question by:vivekj2004
6 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33726545

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

0
 
LVL 2

Expert Comment

by:Algol36
ID: 33726610

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

Expert Comment

by:kris_per
ID: 33726682

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

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33731927
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.
0
 
LVL 4

Accepted Solution

by:
fromer earned 500 total points
ID: 33771020
   public struct UserDefinedArrayObject
    {
        public long LongElement;
        public string StringElement;

        public static int CompareElements(UserDefinedArrayObject x, UserDefinedArrayObject y)
        {
            return x.LongElement.CompareTo(y.LongElement);
        }
        public static void SortArray(UserDefinedArrayObject[] objects)
        {
            Array.Sort(objects, CompareElements);
        }
    }
0
 

Author Closing Comment

by:vivekj2004
ID: 33783534
This was .Net 2.0 code. Traditional code, that's why I chose this as complete solution.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question