c# - 书籍索引的序列和 Rangify 列表

标签 c# sorting indexing

我正在为我的办公室项目编写一个基于 MS word 的注册工具,其中应用程序将根据 Nummer(标题编号、关键字和法律名称)执行复杂的 SR,并为每个输入的 word 文件创建一个注册。

目前应用程序已编码并已完成 90%,对于客户的最新更改请求,我需要将以下内容添加到应用程序中。

目前我有像这样的标题编号列表

1, 2, 3, 3.1, 3.2,3.3,3.4,4,5,6, 7.1.1,7.1.2,7.1.3

要求是按正确的升序对它们进行排序,并Rangify邻近的数字。

例如上面的形式:

1,2,3

应归类为:

1-3

3.1,3.2,3.3,3.4

应归类为:

3.1-3.4

4,5,6

作为

4-5

7.1.1,7.1.2,7.1.3

作为

7.1.1-7.1.3

最终在上面的列表中,项目应该按如下顺序排序和 Rangify :

1-3, 3.1-3.4, 4-6, 7.1.1-7.1.3

我尝试按级别数分隔项目并将它们添加到排序列表并检查距离并将它们置于一个测试范围内,但这对我来说不起作用)

然后通过谷歌搜索,我发现了以下 c# 函数,但该函数仅适用于整数

IEnumerable<string> Rangify(IList<int> input) {
    for (int i = 0; i < input.Count; ) {
        var start = input[i];
        int size = 1;
        while (++i < input.Count && input[i] == start + size)
            size++;

        if (size == 1)
            yield return start.ToString();
        else if (size == 2) {
            yield return start.ToString();
            yield return (start + 1).ToString();
        } else if (size > 2)
            yield return start + " - " + (start + size - 1);
    }
}

所以有人可以指导我为此找到解决方案。

谢谢

最佳答案

你可以这样做:

    private static List<string> SortTitleNums(List<string> titleNums)
    {            
        // list that'll hold the result of current operation
        List<string> result = new List<string>();

        // sorts the input array
        titleNums.Sort();

        // field that will indicate start and end of a sequence
        bool sequenceStarted = false;

        for (int i = 0; i < titleNums.Count - 1; i++)
        {
            // checks if the value is greater than current value by 1 to find sequence
            if (Convert.ToInt32(titleNums[i + 1].Replace(".", "")) - Convert.ToInt32(titleNums[i].Replace(".", "")) == 1)

                // if sequence is found we add this value to the result list and change sequnceStarted field to true.
              { if (!sequenceStarted) { result.Add(titleNums[i] + "-"); sequenceStarted = true; } }

            // if sequence is found and next value does not refer to the sequence than we append the record with current value and change 
            //value for sequenceStarted field to false. If sequence not found than we just add the number.
            else if (sequenceStarted) { result[result.Count - 1] += titleNums[i]; sequenceStarted = false; } else result.Add(titleNums[i]);                
        }

        return result;
    }

使用示例:

    static void Main()
    {
        List<string> titleNums = new List<string>()
        {
            "1", "2", "6", "3", "3.1", "3.2", "3.3", "8", "7.1.1", "7.1.2", "8.1.1", "7.1.3", "7.2.1", 
        };

        titleNums = SortTitleNums(titleNums);

        foreach (var item in titleNums)
            Console.WriteLine(item);

        Console.ReadKey();
    }

输出:“1-3”、“3.1-3.3”、“6”、“7.1.1-7.1.3”、“7.2.1”、“8”

关于c# - 书籍索引的序列和 Rangify 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31648522/

相关文章:

c# - 执行 Asyc 时,我可以 "update"ASP UpdateProgress 吗?下载?

mysql - 找到未使用的索引

c# - C# 中 "static"Random.Next 的正确方法?

c# - 从 Javascript 隐藏代码访问数据

c - 在C中按字典顺序排序

javascript - 检查数组的每个元素

java - Android ArrayList 的 IndexOutOfBoundsException

python - 对大量文件进行近乎重复检查的最佳方法或算法?

c# - 如何在 LINQ lambda 中执行多个表之间的连接

java - 使用比较器对链表进行排序