对1,3,4,6,7,8组合显示成“1,3-4,6-8”方法

/// <summary>
        /// 对期刊装订库的年卷期进行组合
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string QueueString(string input)
        {
            var arr = new System.Collections.ArrayList();
            var lines = input.Split(',').ToList();
            lines = lines.OrderBy(x => int.Parse(x)).ToList();
            int min, max, tmp = 1;
            max = min = int.Parse(lines[0]);
            foreach (var line in lines)
            {
                tmp = int.Parse(line);
                if ((tmp - max) > 1)
                {
                    if (min == max)
                    {
                        arr.Add(string.Format("{0}", max));
                        max = min = tmp;
                    }
                    else
                    {
                        arr.Add(string.Format("{0}-{1}", min, max));
                        max = min = tmp;
                    }
                }
                else
                {
                    max = tmp;
                }
            }
            if ((tmp == min) || (tmp == max))
            {
                arr.Add(string.Format("{0}",max));
            }
            return string.Join(",", arr.ToArray());
        }

 

posted @ 2016-04-20 20:32  我的名称很霸气  阅读(239)  评论(0编辑  收藏  举报