如何在c#中对数字字符串进行排序 [英] How to sort a numeric string in c#

查看:1644
本文介绍了如何在c#中对数字字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





考虑一个简单的字符串,其中包含由昏迷分隔的数字



任何人都可以帮助我按升序排序这个字符串。



示例:string s =20,3,33,44,2;

Hi,

Consider a simple string which contains numbers delimited by coma

Can anyone help me to sort this string in ascending order.

Example : string s = "20,3,33,44,2";

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是你想的很难!



但这真的不难:

1)使用string.Split将数字分开字符串。

2)将每个字符串数转换为整数,整数数组。

3)对数组进行排序。

4)使用ToString将整数转换回字符串

5)使用string.Join重建用逗号分隔的单个字符串。
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But it really isn't difficult:
1) Use string.Split to separate the numbers into separate strings.
2) Convert each string number to an integer, in an array of integers.
3) Sort the array.
4) Convert the integers back to strings using ToString
5) Use string.Join to rebuild the single string separated by commas.


要按排序顺序获取结果,按照数组的简单步骤。



1.通过Split方法将字符串转换为数组

2.制作另一个整数类型的数组相同长度

3.通过转换为整数将所有值放在整数数组中。

4.用for / foreach循环对整数数组进行排序

5.创建一个strin g从排序的整数数组中获得所需的结果。







带有ArrayList的选项2



1.通过Split方法将字符串转换为数组

2.创建ArrayList类的对象。

3将数组的所有值添加到ArrayList对象中。

4.使用Sort方法对ArrayList对象进行排序。 (arrList.Sort())

5.从排序的ArrayList对象创建一个字符串以获得所需的结果
To get the result in sorted order, follow the simple steps of arrays.

1. Convert the string into an array by Split method
2. Make another integer type array of same length
3. put all the values in integer array by converting into integer.
4. Sort the integer array by for/foreach loop
5. Create a string from the sorted integer array to get desired result.



Option 2 with ArrayList

1. Convert the string into an array by Split method
2. Create an object of ArrayList class.
3. Add all the values of array into ArrayList object.
4. Sort the ArrayList object with Sort method. (arrList.Sort())
5. Create a string from the sorted ArrayList object to get desired result


这篇关于如何在c#中对数字字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆