排序数据期间的字符串行为
在我的应用程序中,我从提供人口、景观面积等景观信息的网站下载一些数据。
解析 html 代码后,数据存储在对象列表中,
public class Landscape
{
public int Population { get; set; }
public string Area { get; set; }
}
public List<Landscape> data;
这没有问题。接下来填充 dataGrid,这就是我的应用程序的目的。现在,问题出在哪里?网站上的区域信息采用半数字、半字符串格式。例如:10 000 平方公里。
我可以选择是否修剪 km2 后缀并将数据存储为 int,或者不执行任何操作并将数据存储为字符串。 由于我希望 dataGrid 中的数据保持原始格式,因此我决定不进行修剪。
最后,是否有一种方法将行排序(按区域大小)作为int,尽管它是字符串类型?
In my application I download some data from website providing landscape informations like population, landscape area and few more.
After parsing html code, data are stored in list of objects
public class Landscape
{
public int Population { get; set; }
public string Area { get; set; }
}
public List<Landscape> data;
No problem with that. Follows filling dataGrid and that's purpose of my application. Now, where is the problem? Area info on the website is in semi-numeric, semi-string format. For example: 10 000 km2.
I have possibility to choose whether to trim km2 suffix and store data as int, or do nothing about it and store data as string. Since I want have data in dataGrid in original format, I decided not to trim.
Finally, is there a way to order rows (by size of area) as int although it is string type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 linq 按翻译值排序,但认为这不会很快。
我认为更好的方法是将面积保存为数字并添加修剪部分以供输出。
You can use linq to sort by the translated value but don't think this will be fast.
I think a better way would be to save the Area as a number and add the trimmed part just for output.
您需要实现一个 IComparer 并将其实例传递给采用 IComparer 的 List.Sort() 方法/重载。
这应该可以帮助您开始:
List.Sort
方法 (IComparer
)You will need to implement an IComparer and pass and instance of it to the List.Sort() method/overload that takes an IComparer.
This should get you started:
List<T>.Sort
Method (IComparer<T>
)