如何正确排序小数列表? [英] How do I sort a list of decimals correctly?

查看:128
本文介绍了如何正确排序小数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序使用半正式公式来确定最接近纬度/经度的x个位置。它需要一个基数lat和lon,并将它与xml文件中每个位置元素的lat和lon进行比较,然后将其添加到包含站点lat,site lon和hasrsine结果的自定义List(Result)中(这是一个小数)输入并包含基数和xml条目之间的距离。我把它完全正常运行没有错误但是我认为在控制台中排序后显示的前10位中有一些位置更接近所以我将它移动到wpf并将其绘制到地图上的前10位(Bing地图)控制WPF)并查看了xml文件。我发现有很多位置比前10位更接近底座,所以我得到它将每个hasrsine结果添加到一个新的List(字符串)并打印一个巨大的消息框,每行有1个结果然后我仔细检查了一下,看到它们已被分类,但是不正确:



应该是(这是一个非常简单的例子,真实的是小数,如109.84477432975):

10,50,100,150,200,500

但它是:

10,100,150,200,50, 500



它将它们分成第一个数字组,然后按值对这些组进行排序。当我连续命名文件时,我在文件浏览器中得到它。如果我在单个数字的开头加零,它们只按正确的顺序排序。如何停止此操作并正确执行DECIMALS命令?我无法在计算后更改hasrsine结果,并且我正在对自定义类型列表进行排序:

  public   class 结果
{
public string SiteID { get ; set ; }
public string SiteName { get ; set ; }
public decimal SiteLat { get ; set ; }
public decimal SiteLon { get ; set ; }
public string HavResult { get ; set ; }
}



...使用此:

 results.Sort(委托(结果result1,结果result2){ return  result1.HavResult.CompareTo(result2.HavResult);}); 



我只按HavResult部分对列表进行排序。怎么能正确排序,谢谢。

解决方案

你的问题是你要排序字符串而不是数字

  public   string  HavResult { get ;  set ; } 



首先尝试将其转换为十进制:

 Convert.ToDecimal(result1.HavResult).CompareTo (Convert.ToDecimal(result2.HavResult)); 


您按 HavResult 订购,这是一个字符串,因为50实际上 200 200后按字母顺序, 200 来自 50 。因此,您需要将 HavResult 的类型更改为 double decimal 。不知道该选择什么?然后看看这里 [ ^ ]。更改 HavResult 的类型后,您无需更新排序代码,因为 double 十进制也有一个 CompareTo 方法。


如果你可以构建你的类,那么Haversine value是Decimal类型,然后使用Linq从列表中按升序排序列表非常容易:

  //  < span class =code-comment>除了需要使用其他'using语句外,还必须存在 
使用 System.Collections .Generic;
使用 System.Linq;

// 示例类
public class locationData
{
public < span class =code-keyword> double 纬度;
public double 经度;
public decimal hasrsine;

public locationData( double lat, double lon, decimal haversn)
{
latitude = lat;
经度= lon;
hasrsine = haversn;
}
}

// 假设您创建了一堆实例
// 列表中的locationData< locationdata>
// 名为'locList

// 示例代码,用于创建
的排序列表 // 按字母顺序升序的半胱氨酸值
列表< decimal> dSortList = locList.Select(x = > x.haversine).OrderBy(x = > x ).ToList();


I am writing a program that uses the haversine formula to determine the x nearest locations to a lat/lon. It takes a base lat and lon and compares it to the lat and lon of every location element in an xml file and then it adds it to a custom List(Result) containing the site lat, site lon and haversine result (which is a decimal type and containes the distance between the base and the xml entry). I have it all working fine with no errors but I thought that there were closer locations than some of the top 10 displayed after sorting in the console so I moved it to wpf and got it to plot the top 10 onto a map (the Bing Maps Control for WPF) and also looked at the xml file. I found quite a few locations that were much, much, much closer to the base than the top 10 were so I then got it to add each haversine result to a new List(string) and print a huge messagebox with 1 result on each line and I looked through and saw that they were sorted, but incorrectly:

It should be (this is a very simplified example, the real would be decimals like 109.84477432975):
10, 50, 100, 150, 200, 500
But instead it is:
10, 100, 150, 200, 50, 500

It sorts them into groups of first digits and then sorts those groups by value. I get this in file explorer when I have consecutively named files. They only sort in the correct order if I put a zero at the start of the ones with single digits. How can I stop this and make the DECIMALS order correctly? I cannot change the haversine results once they are calculated and I am sorting my custom type list:

public class Result
{
     public string SiteID { get; set; }
     public string SiteName { get; set; }
     public decimal SiteLat { get; set; }
     public decimal SiteLon { get; set; }
     public string HavResult { get; set; }
}


...by using this:

results.Sort(delegate(Result result1, Result result2) { return result1.HavResult.CompareTo(result2.HavResult); });


And I am sorting the list by only the HavResult part. How can it be sorted correctly, thank you.

解决方案

Your problem is that you are sorting strings not numbers!

public string HavResult { get; set; }


Try to convert it to decimal first:

Convert.ToDecimal(result1.HavResult).CompareTo(Convert.ToDecimal(result2.HavResult));


You order by HavResult, and that's a string, and because 50 actually comes after 200 in alphabetical order, 200 comes before 50. So, you need to change the type of HavResult into double or decimal. Don't know what to choose? Then have a look here[^]. After changing the type of HavResult, you don't need to update your sorting code because double and decimal also have a CompareTo method.


If you can structure your class so that the Haversine value is Type Decimal, then getting a list sorted in ascending order from a List of your Class is very easy using Linq:

// in addition to whatever other 'using statements are required, these must be present
using System.Collections.Generic;
using System.Linq;

// sample class
public class locationData
{
    public double latitude;
    public double longitude;
    public decimal haversine;

    public locationData(double lat, double lon, decimal haversn)
    {
        latitude = lat;
        longitude = lon;
        haversine = haversn;
    }
}

// assume you created a bunch of instances
// of locationData in a List<locationdata>
// named 'locList

// sample code to create a sorted list of
// the haversine values in ascending order
List<decimal> dSortList = locList.Select(x => x.haversine).OrderBy(x => x).ToList();


这篇关于如何正确排序小数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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