Solved

C# array, .Net, dot net

Posted on 2010-09-21
6
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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