繁体   English   中英

如何使用数组列表和循环对 C# 中的 4 个 Colors 进行排序?

[英]How to Sort 4 Colors in C# using Array List and Loops?

我一直在寻找一种按给定顺序对“颜色”或“字符串”进行排序的算法。

这是我的代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            var colors = new ArrayList() { Color.White, Color.Green, Color.Blue, Color.Red};
            int left = 0;
            int right = colors.Count - 1;
            int i = 0;

            while (i <= right)
            {
                Color current = (Color)colors[i];
                if (current == Color.Red)
                {
                    Color temp = current;
                    colors[i] = colors[left];
                    colors[0] = temp;
                    Console.WriteLine(colors[i]);
                    i += 1;
                    left += 1;
                }
                else if (current == Color.Green)
                {
                    Color temp = current;
                    colors[i] = colors[right];
                    colors[right] = temp;
                    Console.WriteLine(colors[i]);
                    right -= 1;
                }
                else if (current == Color.White)
                {
                    Color temp = current;
                    colors[i] = colors[left];
                    colors[left] = temp;
                    Console.WriteLine(colors[i]);
                    i += 1;
                    left += 1;
                }
                else if (current == Color.Blue)
                {
                    Color temp = current;
                    colors[i] = colors[left];
                    colors[left] = temp;
                    Console.WriteLine(colors[i]);
                    i += 1;
                    left += 1;
                }
                //i += 1;
            }
            
        }
    }
}

上面代码的结果是:

Color [White]
Color [Red]
Color [Red]
Color [Blue]

我希望结果是:

Color [Red]
Color [White]
Color [Blue]
Color [Green]

另外,我想将它们推入 arrays,我首先需要知道的是按照给定的顺序对这些颜色/字符串进行排序。 有人可以指导或帮助我吗? 谢谢!

编辑

最近,我的代码是:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            var colors = new ArrayList() { Color.Red, Color.White, Color.Blue, Color.Green };
            int left = 0;
            int right = colors.Count - 1;
            int i = 0;

            while (i <= right)
            {
                Color current = (Color)colors[i];
                if (current == Color.Red)
                {
                    Color temp = (Color)colors[i];
                    colors[i] = colors[left];
                    colors[left] = temp;
                    Console.WriteLine(colors[i]);
                    i += 1;
                    left += 1;
                }
                else if (current == Color.Green)
                {
                    Color temp = (Color)colors[i];
                    colors[i] = colors[right];
                    colors[right] = temp;
                    Console.WriteLine(colors[i]);
                    right -= 1;
                }
                else if (current == Color.White)
                {
                    Color temp = (Color)colors[i];
                    colors[i] = colors[left];
                    colors[left] = temp;
                    Console.WriteLine(colors[i]);
                    i += 1;
                    left += 1;
                }
                else if (current == Color.Blue)
                {
                    Color temp = (Color)colors[i];
                    colors[i] = colors[left];
                    colors[left] = temp;
                    Console.WriteLine(colors[i]);
                    i += 1;
                    left += 1;
                }
                else
                {
                    i += 1;
                }
            }
            
        }
    }
}

结果是我期望的顺序:

Color [Red]
Color [White]
Color [Blue]
Color [Green]

现在,我将 ArrayList 值更改为随机排序的值。 我无法解决问题。

您可以尝试将 colors映射int ,然后比较这些int ,例如

// Map : each Color has its index within order array
// if you want to add Yellow, put it to the right place
Color[] order = new Color[] {
  Color.White, Color.Red, Color.Blue, Color.Green
};

然后有

List<Color> colors = new List<Color>() {
  Color.White, Color.Green, Color.Blue, Color.Red
}

您可以按如下方式对其进行排序:

colors.Sort((a, b) => order.IndexOf(a).CompareTo(order.IndexOf(b)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM