如何比较两个“数字”有多个点? [英] How to compare two "numbers" with multiple dots?

查看:136
本文介绍了如何比较两个“数字”有多个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无序列表,可以是这个样子:

I have an unordered list that can look something like this:

1
结果
2.2
结果
1.1.1
结果
3

1
2.2
1.1.1
3

当我对列表进行排序,1.1.1变得大于3和2.2,和2.2变为大于3

When i sort the list, 1.1.1 becomes greater than 3 and 2.2, and 2.2 becomes greater than 3.

这是因为Double.Parse移除的点,使得它的一个整数。

This is because Double.Parse removes the dots and makes it a whole number.

这是我用排序的方法:

public class CompareCategory: IComparer<Category>
{
    public int Compare(Category c1, Category c2)
    {
        Double cat1 = Double.Parse(c1.prefix);
        Double cat2 = Double.Parse(c2.prefix);

        if (cat1 > cat2)
            return 1;
        else if (cat1 < cat2)
            return -1;
        else
            return 0;
    }
}



我怎样才能解决这个问题?
谢谢

How can i fix this? Thanks

推荐答案

是一个偶然的机会,这些版本#秒?您可以使用版本?你似乎想要的,尽管它只能最多4件排序它每一部分。我不会推荐解析成一个数值,就像你在做什么。

Are these version #s by chance? Can you use the Version class? It sorts each part as you seem to want, although it only works up to 4 parts. I would not recommend parsing into a numeric value like you are doing.

它有一个 IComparable的接口。假设你的投入是字符串,这里有一个例子:

It has an IComparable interface. Assuming your inputs are strings, here's a sample:

public class CompareCategory: IComparer<Category>
{
    public int Compare(Category c1, Category c2)
    {
        var cat1 = new Version(c1.prefix);
        var cat2 = new Version(c2.prefix);

        if (cat1 > cat2)
            return 1;
        else if (cat1 < cat2)
            return -1;
        else
            return 0;
    }
}

如果您需要4个以上的零件的东西,我想我会创建一个在点分裂的字符串,比较器,然后分析每个元素作为一个整数,并将其数值进行比较。确保考虑像1.002.3和1.3.3例(你想要的排序顺序是什么?)。

If you need something with more than 4 "parts", I think I would create a comparer which split the strings at the dots, and then parse each element as an integer and compare them numerically. Make sure to consider cases like 1.002.3 and 1.3.3 (what do you want the sort order to be?).

更新这里是我的意思样本。轻轻测试:

Update, here is a sample of what I mean. Lightly tested:

    public class CategoryComparer : Comparer<Category>
    {
        public override int Compare(Category x, Category y)
        {
            var xParts = x.prefix.Split(new[] { '.' });
            var yParts = y.prefix.Split(new[] { '.' });

            int index = 0;
            while (true)
            {
                bool xHasValue = xParts.Length > index;
                bool yHasValue = yParts.Length > index;
                if (xHasValue && !yHasValue)
                    return 1;   // x bigger
                if (!xHasValue && yHasValue)
                    return -1;  // y bigger
                if (!xHasValue && !yHasValue)
                    return 0;   // no more values -- same
                var xValue = decimal.Parse("." + xParts[index]);
                var yValue = decimal.Parse("." + yParts[index]);
                if (xValue > yValue)
                    return 1;   // x bigger
                if (xValue < yValue)
                    return -1;  // y bigger
                index++;
            }
        }
    }
    public static void Main()
    {
        var categories = new List<Category>()
        {
            new Category { prefix = "1" },
            new Category { prefix = "2.2" },
            new Category { prefix = "1.1.1" },
            new Category { prefix = "1.1.1" },
            new Category { prefix = "1.001.1" },
            new Category { prefix = "3" },
        };

        categories.Sort(new CategoryComparer());
        foreach (var category in categories)
            Console.WriteLine(category.prefix);
    }



输出:

Output:

1
1.001.1
1.1.1
1.1.1
2.2
3

这篇关于如何比较两个“数字”有多个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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