对数字字符串进行排序 [英] Sorting a numeric string

查看:96
本文介绍了对数字字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试对实际上是一个数字字符串的值进行排序。当我尝试使用LINQ对值进行排序时,我无法获得正确的排序数据。



Say.if我按升序对值进行排序{

Hi,
I am trying to sort the values which is actually a seminumeric string.When i try to sort the values using LINQ i cant able to get the proper sorted data.

Say.if i am sorting the value in ascending order{

 "121212-1","121212-7","121212-6","121212-16",121212-4",
"121212-15",
"121212-14"



}

查询.OrderBy(list => list)



我得到正确的输出.KIndly帮我分类数字。



我已经尝试过这里提供的解决方案



[ ^ ] br />
但它没有帮助我...



问候,

Hsakarp。


}
with the query .OrderBy(list => list)

I am getting the proper output.KIndly help me to sort the seminumeric number.

I ahve tried the solution provided here

[^]
but it didnot helped me...

Regards,
Hsakarp.

推荐答案

问题在于您尝试对字符串进行排序,就好像它们是数字一样 - 这不起作用,因为排序顺序不同。对于字符串,排序顺序为:

The problem is that you are trying to sort strings as if they were numbers - that doesn't work, because the sort order is different. With strings, the sort order goes:
1
10
11
12
...
19
2
20
...

你需要做的就是将它们转换为数字。也许,最好的方法是使用一个类,但是快速而肮脏的方法是这样做:

What you need to do is convert them to numbers are sort those. Probably, the best way is to use a class, but a quick and dirty method would be to do this:

List<string> list = new List<string>(){ "121212-1","121212-7","121212-6","121212-16","121212-4","121212-15","121212-14"};
list = list.OrderBy(x => int.Parse(x.Split('-')[0])).ThenBy(x => int.Parse(x.Split('-')[1])).ToList();
foreach (string s in list)
    {
    Console.WriteLine(s);
    }

但是你需要确保每个字符串都是正确的格式,否则你将得到空引用或解析错误!

But you would need to be sure that every string was in the right format first or you will get null reference or parse errors!


你将必须拆分字符串并按整数排序,如下所示:



You'll have to split the strings and order them as integers, like so:

IList<string> list = new List<string> 
    {
        "121212-1",
        "121212-7",
        "121212-6",
        "121212-16",
        "121212-4", 
        "121212-15", 
        "121213-14", 
        "121212-14"
    };
	
    Console.WriteLine("unsorted");
    foreach(string s in list) Console.WriteLine(s);

    /**
        Output:
        
        121212-1
        121212-7
        121212-6
        121212-16
        121212-4
        121212-15
        121213-14
        121212-14
    **/
    
    IList<string> sortedList = list
        .Select(s => s.Split(new[]{"-"}, StringSplitOptions.RemoveEmptyEntries))
        .OrderBy(a => Int32.Parse(a[0]))
        .ThenBy(a => Int32.Parse(a[1]))
        .Select(a => String.Join("-", a))
        .ToList();
    
    Console.WriteLine("sorted");
    foreach(string s in sortedList) Console.WriteLine(s);

    /**
        Ouput:
        
        121212-1
        121212-4
        121212-6
        121212-7
        121212-14
        121212-15
        121212-16
        121213-14
    **/


(如果我找到你)你必须解析每个'seminumeric'项目以便分开两个数字部分(例如121212-1=> fn = 121212,ln = 1 )然后根据这些数字部分对项目进行排序(首先考虑 fn 然后 ln )。

它应该非常简单。
(If I got you) you have to parse each 'seminumeric' item in order to separate the two numeric parts (e.g. "121212-1" => fn=121212, ln=1) and then order the items based on such numeric parts (first considering fn and then ln).
It should be pretty straightforward.


这篇关于对数字字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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