0

Using C#, I have a 2D Jagged array containing objects, what I want to achieve is to sort this Jagged array based on a public property within these objects I have created a sample below of my problem, I have limited experience with using LINQ, but I have attemped using it and have failed, I have also proceeded to create a swap method if needed.

Any insight would be much welcomed, thank you

    //[][] of Objects
    private MyObject[][] jaggedArray = new MyObject[3][]
    {
        new MyObject[5]
        {
            new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
        },
        new MyObject[5]
        {
            new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
        },

        new MyObject[5]
        {
            new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
        },
    };


    static void swap(ref MyObject a, ref MyObject b)
    {
        MyObject temp = a;
        a = b;
        b = temp;
    }

class MyObject
{
    public MyObject()
    { Value = rand.Next(100);    }
    public int Value{ get; set; }

    Random rand = new Random();
}
1
  • That was a mistake on my part, edited Commented Apr 9, 2017 at 12:53

1 Answer 1

1

Code below sorts in one dimension

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            new Test();
        }
    }
    public class Test
    {
        //[][] of Objects
        private MyObject[][] jaggedArray = new MyObject[3][]
        {
            new MyObject[5]
            {
                new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
            },
            new MyObject[5]
            {
                new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
            },

            new MyObject[5]
            {
                new MyObject(), new MyObject(), new MyObject(), new MyObject(), new MyObject(),
            },
        };

        public Test()
        {
            jaggedArray = jaggedArray.Select(x => x.OrderBy(y => y).ToArray()).ToArray();
        }
    }
    public class MyObject : IComparable<MyObject>
    {
        public MyObject()
        { 
            Value = rand.Next(100);    
        }
        public int Value{ get; set; }

        static Random rand = new Random();

        public int CompareTo(MyObject o)
        {

            return this.Value.CompareTo(o.Value);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was able to adapt IComparable<MyObject> along with the CompareTo method to solve my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.