sorting a string
-
I have a string variable which is stored in the form of 4,3,2,1 - is there some way I can sort this list so i can see it as 1,2,3,4 Thank you very much!
string source = "1,2,3,4";
string[] parts = source.Split(',');
Array.Sort(parts);
string result = string.Join(",", parts);NB: This will sort using string comparison. That means that the string
"9"
will come after the string"10"
. If that's not what you want, then you'll need to expand your specification.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I have a string variable which is stored in the form of 4,3,2,1 - is there some way I can sort this list so i can see it as 1,2,3,4 Thank you very much!
Try this:
string reversedString = new string("4,3,2,1".Reverse().ToArray());
Note that this just literally reverses the order of the characters; it doesn't really "sort" in the technical sense of that word.
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
-
I have a string variable which is stored in the form of 4,3,2,1 - is there some way I can sort this list so i can see it as 1,2,3,4 Thank you very much!
LINQ -
var sorted = new List(str.Split(',').Select(s => int.Parse(s))).OrderBy(x=>x);
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial
-
Try this:
string reversedString = new string("4,3,2,1".Reverse().ToArray());
Note that this just literally reverses the order of the characters; it doesn't really "sort" in the technical sense of that word.
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
Just what i needed. Thank you. If you don't mind me asking, how did you learn about this way to solve the problem? How long did it take you to figure out? Instead of creating a new variable, i tried to reverse the existing variable, and put into a ViewState variable. That did not work. so, i went to what you have written, which works!! of the form:
ViewState["questionumber"] = myquestionumber.Reverse().ToArray();
How did you know we have to create a new variable to do this? The fact we are treating it as new string?
-
Just what i needed. Thank you. If you don't mind me asking, how did you learn about this way to solve the problem? How long did it take you to figure out? Instead of creating a new variable, i tried to reverse the existing variable, and put into a ViewState variable. That did not work. so, i went to what you have written, which works!! of the form:
ViewState["questionumber"] = myquestionumber.Reverse().ToArray();
How did you know we have to create a new variable to do this? The fact we are treating it as new string?
vkEE wrote:
How did you know we have to create a new variable to do this? The fact we are treating it as new string?
Funny question actually. My personal evolution in this, is that I started out using new variables and copies for everything. As we tend to divide a problem in smaller problems, transforming data (eg sorting a string) or other problems result in the creation of new variables to keep track of things (solutions to each smaller problem). The more experience you gain through the years, the more compact I would start to develop, creating less and less variables.