简体   繁体   中英

C# Sort Coordinate Points ascending

I was given this C# code to generate 10 random coordinate points. The code generates an ArrayList with the 10 coordinates, prints the objects, sorts them, and prints them again:

using System;
using System.Collections;

namespace Point
{
    class Program
    {

        public class Point
        {
            public int x;
            public int y;

            public Point(int X, int Y)
            {
                x = X;
                y = Y;
            }
            
            override public string ToString()
            {
                return y + "." + x;
            }
            
        }

        public static void PrintValues(IEnumerable myList)
        {
            foreach (Object obj in myList)
                Console.WriteLine("{0}", obj);
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
  
            ArrayList AL = new ArrayList();
            Random R = new Random();
            for(int i = 0; i< 10; i ++)
            {
                Point p = new Point(R.Next(50), R.Next(50));
                AL.Add(p);
            }
            PrintValues(AL);
            AL.Sort();
            PrintValues(AL);

        }
    }
}

To my questions:

  1. Why is the ToString() method in the class Point automatically called even if I dont use p.ToString() ?

  2. How is it possible to compare the Points in this case? Do i need to change the type to int or double?

For question #1, look at the documentation for Console.WriteLine() . There are several overloads, but the one matching the way you call the function expects a string and an object. That overload knows to use the ToString() method on the passed object. You could simplify the line even further like this, which will do the same thing:

Console.WriteLine(obj);

If you had not provided a ToString() method with the Point class, it would have used the one inherited from the base Object type, which by default will print the type name.


For question #2, we again look to the documentation . The link I provided describes the process.Net will use to find a suitable Comparer instance that can be used to sort the items in the collection.

Note if you follow this documentation closely, and the documentation for ArrayList.Sort() on through to the appropriate Array.Sort() overloads, we see there is no suitable comparer available, which should cause this code to fail . I suspect somehow a default comparer is inferred, possibly for the string result from the ToString() overload.


Hopefully you spotted a theme in this answer, and that is read the documentation. Even long-time C# users like myself need to refer back to in constantly.

Finally, I might write that code like this to use a generic list and Point type already included in the.Net.

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;

namespace Point
{
    class Program
    {
        public static void PrintValues(IEnumerable myList)
        {
            foreach (Object obj in myList)
                Console.WriteLine(obj);
            Console.WriteLine();
        }

        private static Random R = new Random();
        static void Main(string[] args)
        {  
            var points = new List<Point>();
            for(int i = 0; i< 10; i ++)
            {
                poinst.Add( new Point(R.Next(50), R.Next(50)));
            }

            PrintValues(points);
            PrintValues(points.OrderBy(p => p.ToString()));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM