# Order Colors String

Given is a `s` colors string. Each color is separated by space and end with integer number, which represents their order. Return a new string with sorted order of colors.

```
Example:
Input: "red3 blue2 green4 white1"
Output: "white blue red green"

```

### Solutions

```csharp
class MyClass
{
    public class LastCharComparer : IComparer<string>
    {
        public int Compare(string? x, string? y)
        {

            if (x is null && y is null)
            {
                return 0;
            }
            if (x is null || y is null)
            {
                return x is null ? -1 : 1;
            }

            var first = x[^1];
            var second = y[^1];

            return first.CompareTo(second);
        }
    }
    public string ColorsNameString(string str)
    {
        var colors = str.Split(' ');
        var orderedColors = new SortedSet<string>(new LastCharComparer());
        StringBuilder sb = new();

        foreach (var item in colors)
        {
            orderedColors.Add(item);
        }

        foreach (var item in orderedColors)
        {
            sb.Append(item[0..(item.Length - 1)]);
            sb.Append(" ");
        }
        return sb.ToString();
    }

}
```

**Time complexity:** O(n log n)

**Space complexity:** O(n)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-57.gitbook.io/data-structure-and-algorithms/problems/sorting/order-colors-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
