Solved

C# array, .Net, dot net

Posted on 2010-09-21
6
471 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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