c# - 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
?
解决方案
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
推荐阅读
- html - 如何使用 HTML 和 CSS 在页面底部制作 2 个固定页脚?
- ubuntu - 下载 mongodb compass 并使用终端安装
- fonts - 如何使用 CFF2 表在 OpenType 中定义没有任何变体的字体
- c# - 创建对象时堆栈溢出错误
- javascript - 总结数组+计算实例
- android - 无法找出针对 SOFTMAX 输入/输出的 Android NNAPI 的 CTS 测试
- javascript - 用户单击行树或表格插件时如何更改数据(无法获取数据中的行索引)?
- android - 一个铿锵的错误:xmmintrin.h 的奇怪用法和未声明的标识符 '__builtin_ia32_emms' 的使用
- javascript - 我无法使用续集创建“研究人员”
- javascript - 因为剧本三度着火