0

I have an array of strings

string[] my_array = {"5:five", "8:eight","4:four", "7:seven","1:one", "6:six"};

I would like to have an output string like the one shown below, such that the values are concatenated in an ascending order

output_string = "onefourfivesixseveneight";

Here is my code

string [] args = {"5:five", "8:eight","4:four", "7:seven","1:one", 
                              "6:six" ,"840"};
            string outputString = "";
           int lowest_divisor = 1;
           int dividend = Convert.ToInt32(args[args.Length - 1]);
           for(int i = 0; i<args.Length - 1; i++)
               {

               string[] pairs = args[i].Split(":");

               int divisor = Convert.ToInt32(pairs[0]);

               string pairString = pairs[1];

               if(i == 0)
               {

                   lowest_divisor = divisor;
                   outputString = pairString;
               }
               else if(divisor <= lowest_divisor)
               {
                   outputString = pairString + outputString;
                   lowest_divisor = divisor;
               }
               else if(divisor > lowest_divisor)
               {
                  outputString = outputString + pairString;


               }
    }
2
  • What's all the divisor stuff doing? What do you get as a result now and how does that differ from what you're expecting. Sorting an array of strings such as you have is as easy as args.Take(args.Length - 1).OrderBy(a => a), but it's not clear what else you want done... Commented May 8, 2019 at 20:41
  • Suppose I have an array of strings: my_array = {"5:five", "8:eight","4:four", "7:seven","1:one", "6:six"}; I'd like to have an output in this format: output_string = "onefourfivesixseveneight" .note the order of the string Commented May 8, 2019 at 20:51

3 Answers 3

4

First we can limit to only the strings in the int:value format

IEnumerable<string> validStrings = my_array.Where(x => x.Contains(':') && int.TryParse(x.Split(':')[0], out int test));

Then we get the list ordered by the integer value

IEnumerable<string> orderedStrings = validStrings.OrderBy(x => int.Parse(x.Split(':')[0]));

Then we can combine them

string combined = string.Join("", orderedStrings.Select(x => x.Split(':')[1]));
Sign up to request clarification or add additional context in comments.

Comments

3

I think that the easiest way to do this is with linq. like this:

string outputString = string.Join("", args.OrderBy(x => int.Parse(x.Split(':')[0])).Select(x => x.Split(':').Length > 1 ? x.Split(':')[1] : ""));

if you don't control the content you can add checking to make sure it doesn't throw exception

Comments

3

This is little bit more verbose approach than the linq solutions provided.

      SortedDictionary<int, string> dict = new SortedDictionary<int, string>();
      foreach (string s in my_array)
       {
            string[] splitArr=s.Split(':');
            dict.Add(Convert.ToInt32(splitArr[0]), splitArr[1]);
        }            

        StringBuilder sb = new StringBuilder();
        foreach (KeyValuePair<int, string> kvp in dict)
        {
            sb.Append(kvp.Value);
        }
        string final=sb.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.