简体   繁体   中英

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

I've been searching about an algorithm that sorts "colors" or "strings" with a given order.

Here's my code

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;
            }
            
        }
    }
}

Result of the code above was:

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

And I want the result to be:

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

Also, I want to push them into arrays, all I need to know first is to sort these colors/strings with the given order. Can someone guide or help me? Thank you!

EDIT

Recently, my code was:

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;
                }
            }
            
        }
    }
}

And the result is what I am expecting order:

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

Now, I changed the ArrayList values into a random ordered one. I can't solve the problem.

You can try mapping colors to int and then compare these int s, eg

// 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
};

then having

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

you can sort it as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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