首页 新闻 搜索 专区 学院

求问同色球不相临的排序算法

0
悬赏园豆:30 [待解决问题]
原数据以颜色主序,id副序排列。
要求结果以Id主序,但同色不相临,求大神拯救。万分感谢。
 
            var balls = new List<ball>()
            {
                new ball() {Id=1,color="red" },
                new ball() {Id=2,color="red" },
                new ball() {Id=3,color="red" },
                new ball() {Id=4,color="green" },
                new ball() {Id=5,color="green" },
                new ball() {Id=6,color="green" },
                new ball() {Id=7,color="black" },
                new ball() {Id=8,color="black" },
                new ball() {Id=9,color="black" }
            };

            //怎么样将balls排序成result??
            var result = new List<ball>()
            {
                new ball() {Id=1,color="red" },
                new ball() {Id=4,color="green" },
                new ball() {Id=7,color="black" },
                new ball() {Id=2,color="red" },
                new ball() {Id=5,color="green" },
                new ball() {Id=8,color="black" },
                new ball() {Id=3,color="red" },
                new ball() {Id=6,color="green" },
                new ball() {Id=9,color="black" },
            };

 

丶飘零丶的主页 丶飘零丶 | 初学一级 | 园豆:98
提问于:2016-05-24 23:21
< >
分享
所有回答(2)
0

只想到了个笨方法

            var r = (from b in balls
                    where b.color == "red" 
                    orderby b.Id
                    select b).ToList() ;
            var g = (from b in balls
                    where b.color == "green"
                    orderby b.Id
                    select b).ToList() ;
            var k = (from b in balls
                    where b.color == "black"
                    orderby b.Id
                    select b).ToList() ;
            List<ball> result = new List<ball>();
            for (int i = 0; i < r.Count; i++)
            {
                result.Add(r[i]);
                result.Add(g[i]);
                result.Add(k[i]);
            }

 

plcly | 园豆:206 (菜鸟二级) | 2016-05-25 09:25

这肯定不行,实际情况颜色不确定,各颜色数量不确定怎么办。

支持(0) 反对(0) 丶飘零丶 | 园豆:98 (初学一级) | 2016-05-25 10:54

@丶飘零丶: 

太丑,将就看下吧

var balls = new List<ball>()
            {
                new ball() {Id=1,color="red" },
                new ball() {Id=2,color="red" },
                new ball() {Id=3,color="red" },
                new ball() {Id=4,color="green" },
                new ball() {Id=5,color="green" },
                new ball() {Id=6,color="green" },
                //new ball() {Id=7,color="black" },
                //new ball() {Id=8,color="black" },
                new ball() {Id=9,color="black" }
            };
            var c = (from b in balls group b.color by b.color into g select g.Key).ToList();
            List<List<ball>> colorList = new List<List<ball>>();
            foreach (var cc in c)
            {
                colorList.Add((from b in balls where b.color == cc.ToString() select b).ToList());
            }
            int maxLength = 0;
            foreach (var n in colorList)
            {
                if (n.Count > maxLength)
                {
                    maxLength = n.Count;
                }
            }
            List<ball> result = new List<ball>();
            for (int i = 0; i < maxLength; i++)
            {
                for (int j = 0; j < c.Count; j++)
                {
                    if (i < colorList[j].Count)
                    {
                        result.Add(colorList[j][i]);
                    }
                }
            }

 

支持(1) 反对(0) plcly | 园豆:206 (菜鸟二级) | 2016-05-25 13:40

@plcly: 这个依然不能解决当同一色的太多的时候尾数的问题。

支持(0) 反对(0) 丶飘零丶 | 园豆:98 (初学一级) | 2016-06-06 12:51

@丶飘零丶: 这个我已经去掉了最后两个排序了,结果是1492536,你需要的结果是什么,不如你提供一个复杂的,用这段程序试一下,哪里不一样

支持(0) 反对(0) plcly | 园豆:206 (菜鸟二级) | 2016-06-07 10:45

@plcly: 

            var balls = new List<Ball>()
            {
                new Ball() {Id=1,color="red" },
                new Ball() {Id=2,color="red" },
                new Ball() {Id=3,color="red" },
                new Ball() {Id=3,color="red" },
                new Ball() {Id=3,color="red" },
                new Ball() {Id=3,color="red" },
                new Ball() {Id=3,color="red" },
                new Ball() {Id=4,color="black" },
                new Ball() {Id=5,color="black" },
                new Ball() {Id=6,color="black" },
                new Ball() {Id=7,color="green" },
                new Ball() {Id=8,color="green" },
                new Ball() {Id=9,color="green" }
            };
View Code

假如我一种色很多的话,就有问题了

支持(0) 反对(0) 丶飘零丶 | 园豆:98 (初学一级) | 2016-06-07 12:40
0

先获取原表中同一颜色的数量colorCount 你这里是3

循环抽取数据组成新表,规律是:

两层嵌套循环

int ballsIndex=0;

for(int i=0;i<colorCount;i++)

{

for(int j = i;j<balls.Count;j+= colorCount)

{

result[j]=balls[ballsIndex];

ballsIndex++;

}

}

刘宏玺 | 园豆:14004 (专家六级) | 2016-05-25 09:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册