0

I'm trying to follow some example in learning sorting arrays. Example use Id as integer to sort by this property, since my object use Guid datatype instead of int I'm decided to use Created DateTime property to sort array. Here's the code

Car.cs
    public class Car:ICar,IComparable
    {
       ... properties
       int IComparable.CompareTo(object)
       {
          Car temp = obj as Car;
          if (temp != null)
          {
             if (this.Created > temp.Created)
                return 1;
             if (this.Created < temp.Created)
                return -1;
            else 
                return 0;
          }
          else {throw new ArgumentException("Parameter is not a Car object");}
       }
    }

Garage.cs

public class Garage : IEnumerable
    {
        private Car[] cars = new Car[4];

        public Garage()
        {
            cars[0] = new Car() { Id = Guid.NewGuid(), Name = "Corolla", Created = DateTime.UtcNow.AddHours(3), CurrentSpeed = 90 };
            cars[1] = new Car() { Id = Guid.NewGuid(), Name = "Mazda", Created = DateTime.UtcNow.AddHours(2), CurrentSpeed = 80 };
        }
       ...
}

Program.cs

 static void Main(string[] args)
        {
           Garage cars = new Garage();
           Console.WriteLine("sorting array:");
           Array.Sort(cars); // error occured
        }

Error 2 Argument 1: cannot convert from 'Car' to 'System.Array'

5
  • 2
    Um, your last two lines appear not to be in a method. Could you please give your actual code which is causing a problem? Commented Nov 13, 2012 at 9:24
  • Also you cannot have private inside methods, agree with Jon, need to see your actual code Commented Nov 13, 2012 at 9:28
  • Also, your cars array has two null's at the end of the array. Commented Nov 13, 2012 at 9:29
  • @Jon Skeet here is my actual code. Commented Nov 13, 2012 at 9:39
  • Is the message really 'from Car to Array'? It would seem it can't convert the Garage to an Array, as the Array.Sort requires an array, which the Garage object is not according to this code. Commented Nov 13, 2012 at 9:47

2 Answers 2

1

This solution assumes you want to sort the array in place, and not create a new one.

To your Garage class, add a method (so you don't have to expose the inner array):

public void Sort() {
    Array.Sort(cars);
}

Then call this method from your program:

static void Main(string[] args)
{
    Garage garage = new Garage();
    garage.Sort();
    //It should be sorted now if you enumerate over it!
}

I've taken the liberty of calling your Garage object garage rather than cars which can be confusing.

I haven't tested this, but it's so straightforward, that it should work.

Sign up to request clarification or add additional context in comments.

Comments

0

Your private Car[] cars is defined within a block (between curly braces) and you reference it outside of that block when you write Array.Sort(cars).

So somewhere you define a variable cars that is of the wrong type.

If you do the Array.Sort in the same block where you define the array, it will work.

2 Comments

@Roy the variable cars is actually of type Garage if you read the code, so that is not true. Performing it on the cars field of the Garage object would indeed not be possible, beacse it is private. That would be the way to go if they wanted to use Array.Sort though.
@RenéWolferink: the code examples above have changed since my answer.

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.