Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. sorting a string

sorting a string

Scheduled Pinned Locked Moved C#
algorithms
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vkEE
    wrote on last edited by
    #1

    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!

    Richard DeemingR B A 3 Replies Last reply
    0
    • V vkEE

      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!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • V vkEE

        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!

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        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

        V 1 Reply Last reply
        0
        • V vkEE

          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!

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          LINQ - var sorted = new List(str.Split(',').Select(s => int.Parse(s))).OrderBy(x=>x);

          Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

          1 Reply Last reply
          0
          • B BillWoodruff

            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

            V Offline
            V Offline
            vkEE
            wrote on last edited by
            #5

            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?

            V 1 Reply Last reply
            0
            • V vkEE

              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?

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              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.

              V.
              (MQOTD Rules and previous Solutions )

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups