C#使用对象外参数对对象进行排序 [英] C# sorting objects with out-of object parameters

查看:97
本文介绍了C#使用对象外参数对对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据到给定x,y点的距离对x,y点的列表进行排序.如何在C#中做到这一点?

我注意到基于C#标准IComparable接口的排序算法不起作用,因为CompareTo函数将一个x,y点与另一个点进行比较,并且无法访问计算距离所需的第三个点.

还有其他一些C#排序库可以做到这一点吗?

我也发现了这一点,但是我不太确定它是如何工作的:

I need to sort a list of x,y points based on distance to a given x,y point. How to do this in C#?

I noticed that the C# standard IComparable interface based sort algorithm does not work because the CompareTo function compares one x,y point to another and there is no way to give access to the third point needed to calculate the distance.

Is there some other C# sorting library that can do this?

I also found this, but I''m not quite sure how it works:

public Comparison<T> MakeComparison<T>(object extraParameter)
{
    return
        delegate(T x, T y)
        {
            // do comparison with x, y and extraParameter
        }
}

推荐答案

比这简单一点!
本示例仅通过查看X距离对点列表进行排序:
It''s a bit simpler than that!
This example sorts a list of Points by looking only at the X distance:
        List<Point> p = new List<Point>();
        ... // Fill p with Points!
        p.Sort(new PointComparer());
        ...
public class PointComparer : IComparer<Point>
    {
    #region IComparer<Point> Members
    public int Compare(Point x, Point y)
        {
        return x.X - y.X;
        }
    #endregion
    }



达尼特!再次被HTML标记抓住了! -OriginalGriff [/edit]

[edit2]点"更改为点":O-OriginalGriff [/edit2]



[edit]Damnit! Caught by the HTML tags again! - OriginalGriff[/edit]

[edit2]"point" changed to "Point" :O - OriginalGriff[/edit2]


好,找到了对我来说或多或少可以的解决方案.我使用了另一个类point_sorter来保存第三点,并定义了一个comparment< T>在那里委托兼容功能以比较两点.

Ok, found a solution that looks more or less ok to me. I used another class, point_sorter to hold the third point and defined a Comparison<T> Delegate compatible function there to compare the two points.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace point
{
    class point
    {
        public double x;
        public double y;
        public point(double px, double py)
        {
            x = px;
            y = py;
        }
    }
    class Program
    {
        class point_sorter
        {
            point p3;
            public point_sorter(point p_p3)
            {
                p3 = p_p3;
            }
            public int compare_points(point p1, point p2)
            {
                double distance_this = (p3.x - p1.x) * (p3.x - p1.x) + (p3.y - p1.y) * (p3.y - p1.y);
                double distance_other = (p3.x - p2.x) * (p3.x - p2.x) + (p3.y - p2.y) * (p3.y - p2.y);
                return distance_this.CompareTo(distance_other);
            }
        }
        static void Main(string[] args)
        {
            List<point> point_list = new List<point>();
            point_list.Add(new point(1, 2));
            point_list.Add(new point(3, 4));
            point_list.Add(new point(5, 5));
            point p3 = new point(6, 6);
            point_sorter ps = new point_sorter(p3);
            point_list.Sort(ps.compare_points);
        }
    }
}


查看此替代方法是否可以帮助您.

see if this alternate can help..

class Program
   {
       public class User
       {
           public string Name;
           public int Age;

           public User(string name, int age)
           {
               this.Name = name;
               this.Age = age;
           }
       }

       static void Main(string[] args)
       {
           // array of custom type
           User[] users = new User[3] { new User("Betty", 23),  // name, age
                            new User("Susan", 20),
                            new User("Lisa", 25) };
           //The distance property
           int customVariable = 10;
           Array.Sort(users, delegate(User user1, User user2)
           {
               //the customVariable is accessable here
               int theCustomVariable = customVariable;
               //Write your logic here to compare it using customVariable
               return user1.Age.CompareTo(user2.Age);
           });
           // write array output
           foreach (User user in users) Console.Write(user.Name + user.Age + " ");
       }
   }


同时,我会提供精确的解决方案:)

谢谢,
Hemant


i''ll for exact solution meanwhile :)

Thanks,
Hemant


这篇关于C#使用对象外参数对对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆