Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的坐标字符串排序#_C#_String_Sorting_Latitude Longitude - Fatal编程技术网

C# c语言中的坐标字符串排序#

C# c语言中的坐标字符串排序#,c#,string,sorting,latitude-longitude,C#,String,Sorting,Latitude Longitude,我现在有一个需要排序的坐标列表。每条线代表经度、纬度。我只需要按经度排序。 它存储在字符串数组中: 字符串[]coords=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 -

我现在有一个需要排序的坐标列表。每条线代表经度、纬度。我只需要按经度排序。 它存储在字符串数组中: 字符串[]coords=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
如何在代码中实现这一点?任何帮助都会很好

解决方案:

我将其调整为以下内容,它正在工作。非常感谢!:)

公共类LongLatSort:IComparer
{
int IComparer.Compare(对象x、对象y)
{
字符串[]longLatParts1=Convert.ToString(x).Split(',');
字符串[]longLatParts2=Convert.ToString(y).Split(',');
var var1=double.Parse(longLatParts1[0]);
var var2=double.Parse(longLatParts2[0]);
if(var1>var2)
{
return-1;//翻转以进行降序
}
else if(var1var2?-1:1;//翻转降序
}
}

此数据是如何存储的?字符串数组?还是二维数组或浮点数?或者是一个具有lat和long的结构数组?我假设它是一个LongLat数组,因为你是这样写的

EDIT我意识到你的主题标题是字符串,所以我添加了一个构造函数来将字符串转换为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() );
//这是假装您正在使用的数据结构
公共级朗拉特{
私人浮动长度;
私人浮动利率;
//为方便起见,从字符串中选择构造函数
公共LongLat(字符串longLatString){
string[]longLatParts=longLatString.Split(',');
mlongalite=float.Parse(longLatParts[0]);
mLatitude=float.Parse(longLatParts[1]);
}
公共浮点经度{get;set;}
公共浮点纬度{get;set;}
}
//分拣机
公共类LongLatSort:IComparer{
public int IComparer.Compare(对象a、对象b){
LongLat o1=(LongLat)a;
LongLat o2=(LongLat)b;
如果(o1.经度>o2.经度){
return-1;//翻转以进行降序
}否则如果(o1.纬度o2.纬度?-1:1;//翻转以进行下降
}
}
//现在你应该可以使用这样的分拣机了?
//虽然最好不要每次都实例化比较器,但你已经明白了
//编辑:首先从字符串创建longlat数组
Sort(youraryoflonglats,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);
    }
}

你能提供任何源代码吗?:)你可能想研究一下类的使用,你所尝试的精确编程问题是什么?(请包括您的代码)看起来像是坐标对象和气泡数组的候选对象sort@user1666620你在开玩笑吧?是的,这是一个列表。字符串的格式为经度、纬度。谢谢。对于此解决方案,列表坐标已转换为字符串[]坐标Hanks Aaron.:)
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);
    }
}