110,538
社区成员
发帖
与我相关
我的任务
分享
string[,] zb = new string[10,2];
zb[0, 0] = "10";//这个是编号
zb[0, 1] = "100,100";//这个是坐标
zb[1, 0] = "12";
zb[1, 1] = "56,180";
zb[2, 0] = "5";
zb[2, 1] = "76,55";
zb[3, 0] = "11";
zb[3, 1] = "87,86";
......
static void Main(string[] args)
{
var zb = new List<dot>()
{
new dot(){id = 10, x = 100, y = 100},
new dot(){id = 12, x = 56, y = 180},
new dot(){id = 5, x= 76, y = 55},
new dot(){id = 11, x= 87, y = 86},
};
//按欧氏距离排序
var o = zb.OrderBy(t => Math.Sqrt(t.x * t.x + t.y * t.y)).ToList();
foreach (var t in o) Console.WriteLine(t.id);
//按曼哈顿距离排序
var m = zb.OrderBy(t => t.x + t.y).ToList();
foreach (var t in m) Console.WriteLine(t.id);
}
struct dot
{
public int id;
public int x;
public int y;
}
List<double> lst = new List<double>() { 2.1, 0.2, 1.1, 9.9, 8.88, 1.11 };
lst.Sort();
var a = new int[,]
{
{10, 100, 100},
{12, 56, 180},
{ 5, 76, 55},
{11, 87, 86}
};
a = MultiSort(a, 1, 2);
for (var i = 0; i < a.GetLongLength(0); i++)
{
for (var j = 0; j < a.GetLongLength(1); j++) Console.Write("{0}\t", a[i, j]);
Console.WriteLine();
}
static int[,] MultiSort(int[,] a, params int[] p)
{
var len = a.GetLongLength(0);
var b = new int[len];
for (var i = 0; i < len; i++) b[i] = i;
for (var q = 0; q < p.Length; q++)
{
var k = p[q];
for (var i = 0; i < b.Length - 1; i++)
{
for (var j = 0; j < b.Length - 1 - i; j++)
{
if (a[b[j], k] > a[b[j + 1], k])
{
int c = b[j];
b[j] = b[j + 1];
b[j + 1] = c;
}
}
}
}
var res = new int[len, a.GetLongLength(1)];
for (var i = 0; i < b.Length; i++)
{
for (var j = 0; j < a.GetLongLength(1); j++)
{
res[i, j] = a[b[i], j];
}
}
return res;
}
class zb
{
public class piont {
public int x { get; set; }
public int y { get; set; }
}
public string id { get; set; }
}
有了对象类型后请自行将你的对象序列化为你自己定义的新类型。
接下来我们看看怎么确定物体的坐标距离自己近。
首先自己肯定也要有个坐标,自己不在坐标系内是不可能获取这些物体距离自己的距离的,所以自己肯定也有个 piont 属性,有x和y的值。
然后物体的坐标是x1 y1,这时候物体距离自己的距离就可以使用 两点间距离公式 来确定了:
用了百度知道的图片,侵删。
接下来排序距离值就可以按距离排序了。