c#中的排序坐标字符串
Sorting Coordinate string in c#
我目前有一个需要排序的坐标列表。每条线代表经度、纬度。我只需要按经度排序。
它存储在一个字符串数组中:
字符串 [] 坐标 = fpdp.Coordinates.ToArray();
这是原始列表:
**LongLat**
98.63,85.02
43.08,79.07
26.97,70.88
18.8,62.3
13.47,53.5
8.57,44.8
3.58,36.35
-1.63,28.2
-6.93,20.33
-12.12,12.63
-17.17,5.02
-22.63,-2.25
-28.22,-9.43
-34.98,-15.7
-42.67,-21.08
-51.18,-25.62
-60.55,-29.12
-70.7,-31.12
-81.2,-31.18
-91.42,-29.72
-101.02,-26.97
-109.62,-22.85
-117.3,-17.83
-123.9,-11.9
-129.32,-5.05
-133.55,2.47
-136.9,10.3
-140.45,17.78
-144.75,24.98
-148.6,32.53
-152.02,40.37
-155.85,48.28
-160.8,56.27
-165.75,64.48
-172.62,72.78
171.35,80.83
98.93,85.17
这是我需要的样子。正数按从大到小排序,负数按从小到大排序。只关注第一个经度坐标:
**LongLat-Sorted**
171.35,80.83
98.93,85.17
98.63,85.02
43.08,79.07
26.97,70.88
18.8,62.3
13.47,53.5
8.57,44.8
3.58,36.35
-1.63,28.2
-6.93,20.33
-12.12,12.63
-17.17,5.02
-22.63,-2.25
-28.22,-9.43
-34.98,-15.7
-42.67,-21.08
-51.18,-25.62
-60.55,-29.12
-70.7,-31.12
-81.2,-31.18
-91.42,-29.72
-101.02,-26.97
-109.62,-22.85
-117.3,-17.83
-123.9,-11.9
-129.32,-5.05
-133.55,2.47
-136.9,10.3
-140.45,17.78
-144.75,24.98
-148.6,32.53
-152.02,40.37
-155.85,48.28
-160.8,56.27
-165.75,64.48
-172.62,72.78
如何在代码中完成此操作?任何帮助都会很棒。
解决方案:
我将其调整为以下内容,它正在运行。非常感谢! :)
public class LongLatSort : IComparer
{
int IComparer.Compare(Object x, Object y)
{
string[] longLatParts1 = Convert.ToString(x).Split(',');
string[] longLatParts2 = Convert.ToString(y).Split(',');
var var1 = double.Parse(longLatParts1[0]);
var var2 = double.Parse(longLatParts2[0]);
if (var1 > var2)
{
return -1; // flipped for descending
}
else if (var1 < var2)
{
return 1; // flipped for descending
}
// secondary sort on latitude when values are equal
return var1 > var2 ? -1 : 1; // flipped for descending
}
}
这些数据是如何存储的?字符串数组?或二维数组或浮点数?或具有纬度和经度的某种结构的数组?我假设它是一个 LongLat 数组,因为这就是你的措辞。
编辑 我意识到你的主题标题说的是字符串,所以我添加了一个构造函数来将字符串转换为 LongLat。
您想要的结果看起来是按经度降序排列的。
此代码未经测试,如果不是 100%,请原谅我,但您明白了。
// This is pretending to be the data structure you are using
public class LongLat {
private float mLongitude;
private float mLatitude;
// constructor from string for convenience
public LongLat(string longLatString ) {
string[] longLatParts = longLatString.Split(',');
mLongitude = float.Parse(longLatParts[0]);
mLatitude = float.Parse(longLatParts[1]);
}
public float Longitude {get; set; }
public float Latitude {get; set; }
}
// The sorter
public class LongLatSort : IComparer {
public int IComparer.Compare(object a, object b) {
LongLat o1=(LongLat)a;
LongLat o2=(LongLat)b;
if (o1.Longitude > o2.Longitude) {
return -1; // flipped for descending
} else if ( o1.Latitude < o2.Longitude ) {
return 1; // flipped for descending
}
// secondary sort on latitude when values are equal
return o1.Latitude > o2.Latitude ? -1 : 1; // flipped for descending
}
}
// now you should be able to use the sorter something like this?
// though best to not instantiate the Comparer every time but you get the idea
// EDIT: create your array of LongLats from strings first
Arrays.Sort( yourArrayofLongLats, new LongLastSort() );
刚刚完成测试,似乎可以工作。
class SimplePoint
{
public SimplePoint(string coord)
{
var coords = coord.Split(',').Select(s => double.Parse(s, System.Globalization.CultureInfo.InvariantCulture)).ToArray();
X = coords[0];
Y = coords[1];
}
public double X;
public double Y;
public override string ToString()
{
return X.ToString() + "," + Y.ToString();
}
}
static class LongLatParseAndSort
{
public static string SortedLongLat(string unsorted)
{
return unsorted
.Split(' ')
.Select(c => new SimplePoint(c))
.OrderByDescending(sp => sp.X)
.Select(sp => sp.ToString())
.Aggregate((a, b) => a += b);
}
}
我目前有一个需要排序的坐标列表。每条线代表经度、纬度。我只需要按经度排序。 它存储在一个字符串数组中: 字符串 [] 坐标 = fpdp.Coordinates.ToArray();
这是原始列表:
**LongLat**
98.63,85.02
43.08,79.07
26.97,70.88
18.8,62.3
13.47,53.5
8.57,44.8
3.58,36.35
-1.63,28.2
-6.93,20.33
-12.12,12.63
-17.17,5.02
-22.63,-2.25
-28.22,-9.43
-34.98,-15.7
-42.67,-21.08
-51.18,-25.62
-60.55,-29.12
-70.7,-31.12
-81.2,-31.18
-91.42,-29.72
-101.02,-26.97
-109.62,-22.85
-117.3,-17.83
-123.9,-11.9
-129.32,-5.05
-133.55,2.47
-136.9,10.3
-140.45,17.78
-144.75,24.98
-148.6,32.53
-152.02,40.37
-155.85,48.28
-160.8,56.27
-165.75,64.48
-172.62,72.78
171.35,80.83
98.93,85.17
这是我需要的样子。正数按从大到小排序,负数按从小到大排序。只关注第一个经度坐标:
**LongLat-Sorted**
171.35,80.83
98.93,85.17
98.63,85.02
43.08,79.07
26.97,70.88
18.8,62.3
13.47,53.5
8.57,44.8
3.58,36.35
-1.63,28.2
-6.93,20.33
-12.12,12.63
-17.17,5.02
-22.63,-2.25
-28.22,-9.43
-34.98,-15.7
-42.67,-21.08
-51.18,-25.62
-60.55,-29.12
-70.7,-31.12
-81.2,-31.18
-91.42,-29.72
-101.02,-26.97
-109.62,-22.85
-117.3,-17.83
-123.9,-11.9
-129.32,-5.05
-133.55,2.47
-136.9,10.3
-140.45,17.78
-144.75,24.98
-148.6,32.53
-152.02,40.37
-155.85,48.28
-160.8,56.27
-165.75,64.48
-172.62,72.78
如何在代码中完成此操作?任何帮助都会很棒。
解决方案:
我将其调整为以下内容,它正在运行。非常感谢! :)
public class LongLatSort : IComparer
{
int IComparer.Compare(Object x, Object y)
{
string[] longLatParts1 = Convert.ToString(x).Split(',');
string[] longLatParts2 = Convert.ToString(y).Split(',');
var var1 = double.Parse(longLatParts1[0]);
var var2 = double.Parse(longLatParts2[0]);
if (var1 > var2)
{
return -1; // flipped for descending
}
else if (var1 < var2)
{
return 1; // flipped for descending
}
// secondary sort on latitude when values are equal
return var1 > var2 ? -1 : 1; // flipped for descending
}
}
这些数据是如何存储的?字符串数组?或二维数组或浮点数?或具有纬度和经度的某种结构的数组?我假设它是一个 LongLat 数组,因为这就是你的措辞。
编辑 我意识到你的主题标题说的是字符串,所以我添加了一个构造函数来将字符串转换为 LongLat。
您想要的结果看起来是按经度降序排列的。
此代码未经测试,如果不是 100%,请原谅我,但您明白了。
// This is pretending to be the data structure you are using
public class LongLat {
private float mLongitude;
private float mLatitude;
// constructor from string for convenience
public LongLat(string longLatString ) {
string[] longLatParts = longLatString.Split(',');
mLongitude = float.Parse(longLatParts[0]);
mLatitude = float.Parse(longLatParts[1]);
}
public float Longitude {get; set; }
public float Latitude {get; set; }
}
// The sorter
public class LongLatSort : IComparer {
public int IComparer.Compare(object a, object b) {
LongLat o1=(LongLat)a;
LongLat o2=(LongLat)b;
if (o1.Longitude > o2.Longitude) {
return -1; // flipped for descending
} else if ( o1.Latitude < o2.Longitude ) {
return 1; // flipped for descending
}
// secondary sort on latitude when values are equal
return o1.Latitude > o2.Latitude ? -1 : 1; // flipped for descending
}
}
// now you should be able to use the sorter something like this?
// though best to not instantiate the Comparer every time but you get the idea
// EDIT: create your array of LongLats from strings first
Arrays.Sort( yourArrayofLongLats, new LongLastSort() );
刚刚完成测试,似乎可以工作。
class SimplePoint
{
public SimplePoint(string coord)
{
var coords = coord.Split(',').Select(s => double.Parse(s, System.Globalization.CultureInfo.InvariantCulture)).ToArray();
X = coords[0];
Y = coords[1];
}
public double X;
public double Y;
public override string ToString()
{
return X.ToString() + "," + Y.ToString();
}
}
static class LongLatParseAndSort
{
public static string SortedLongLat(string unsorted)
{
return unsorted
.Split(' ')
.Select(c => new SimplePoint(c))
.OrderByDescending(sp => sp.X)
.Select(sp => sp.ToString())
.Aggregate((a, b) => a += b);
}
}