Solved

C# array, .Net, dot net

Posted on 2010-09-21
6
451 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now