Linq对字符串进行排序

Byy*_*yyo 6 c# linq string

假设我有以下输入:

string input = "123456789";
Run Code Online (Sandbox Code Playgroud)

并期望以下输出:

string output = "741852963";
Run Code Online (Sandbox Code Playgroud)

逻辑是一个正方形,需要向右旋转90度 - 但没有换行符.

//  squares
//INPUT     OUTPUT
// 123  ???  741
// 456    V  852
// 789       963
Run Code Online (Sandbox Code Playgroud)

宽度应始终是动态的

int width = (int)Math.Sqrt(input.Length);
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法来解决这个问题吗?

ASh*_*ASh 11

我个人更喜欢for循环,但也有一个Linq解决方案

尝试与小提琴

string input = "0123456789ABCDEF";
/*
    0123
    4567
    89AB
    CDEF
*/              

int width = (int)Math.Sqrt(input.Length);

var seq = input.AsEnumerable()
                .Select((c, i) => new {Chr = c, Row = i / width, Col = i % width})
                .OrderBy(a => a.Col)
                .ThenByDescending(a => a.Row)
                .Select(a=>a.Chr);
var s = string.Join("", seq);
Console.WriteLine(s);
Run Code Online (Sandbox Code Playgroud)

版画

C840D951EA62FB73
Run Code Online (Sandbox Code Playgroud)