Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
4.80/5 (2 votes)
See more:
I have string like this a12,a11,a20,a45
now i want to sort this string into the following format
a11,a12,a20,a45 how i will do this in asp.net
Posted

Depends - if the string is literally:
C#
string s = "a12,a11,a20,a45";
Then the first thing to do is separate the items you want to sort. String.Split will do that:
C#
string s = "a12,a11,a20,a45";
string[] parts = s.Split(',');

You can then sort the parts:
C#
string s = "a12,a11,a20,a45";
string[] parts = s.Split(',');
Array.Sort(parts);

And finally, rebuild the string:
C#
string s = "a12,a11,a20,a45";
string[] parts = s.Split(',');
Array.Sort(parts);
s = string.Join(",", parts);
 
Share this answer
 
Split your string to create a string array:
C#
string[] strArr = yourStr.Split(',');

then sort the array and recreate the desired string:
C#
Array.Sort(strArr);
string.Join(",", strArr);

Now you have a sorted string formatted.

An nice alternative to the array sort, using LINQ can be:
C#
var sort = from s in strArr
           orderby s
           select s;

Cheers,
c
 
Share this answer
 
v4
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900