首页 > 解决方案 > Sort a String consisting of letters and numbers

问题描述

I have some strings, like below

a1
c1
b0

I want to sort them to a1b0c1

I tried the code below:

string answer1 = new string(answer.OrderBy(c => c).ToArray());

But the result is 011abc

How to make the string a1b0c1?

标签: c#string

解决方案


I assume, you have one string consisting of several lines. You have to split your string into an array, sort this and join the lines together:

string input = "a1\nc1\nb0";
string[] lines = input.Split('\n');
string result = string.Join("\n", lines.OrderBy(x => x));

Online demo: https://dotnetfiddle.net/u32Pk9


推荐阅读