Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# array, .Net, dot net

Posted on 2010-09-21
6
Medium Priority
?
514 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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