Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:58:04+00:00 2026-06-15T21:58:04+00:00

I have a string that might look something like this: 3, 7, 12-14, 1,

  • 0

I have a string that might look something like this: “3, 7, 12-14, 1, 5-6”

What i need to do is to change that into a string looking like this: “1, 3, 5, 6, 7, 12, 13, 14”

I have made the following code work, but i would very much appreciated help how to do this a cleaner way with less lines of code:

private string sortLanes(string lanesString)
    {
        List<string> sortedLanes = new List<string>();

        if (lanesString.Contains(',') || lanesString.Contains('-'))
        {
            List<string> laneParts = lanesString.Split(',').ToList();

            foreach (string lanePart in laneParts)
            {
                if (lanePart.Contains('-'))
                {
                    int splitIndex = lanePart.IndexOf('-');
                    int lanePartLength = lanePart.Length;

                    int firstLane = Convert.ToInt32(lanePart.Substring(0, splitIndex));
                    int lastLane = Convert.ToInt32(lanePart.Substring(splitIndex + 1, lanePartLength - splitIndex - 1));
                    while (firstLane != lastLane)
                    {
                        sortedLanes.Add(firstLane.ToString().Trim());
                        firstLane++;
                    }
                    sortedLanes.Add(lastLane.ToString());
                }
                else
                {
                    sortedLanes.Add(lanePart.Trim());
                }
            }
            sortedLanes.Sort();
            sortedLanes = sortedLanes.OrderBy(x => x.Length).ToList();

            lanesString = "";
            foreach (string lane in sortedLanes)
            {
                if (lanesString.Length == 0)
                {
                    lanesString = lane;
                }
                else
                {
                    lanesString = lanesString + ", " + lane;
                }
            }
        }
        else
        {
            return lanesString;
        }
        return lanesString;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:58:04+00:00Added an answer on June 15, 2026 at 9:58 pm

    I would first split by the , then convert each value into either a single integer or the desired range. Take the results and reorder them and then concatenate back into a string. Something like this.

        string test = "3, 7, 12-14, 1, 5-6";
        var items = test.Split(',');
        var ints = items.SelectMany(item => Expand(item));
        string result = string.Join(", ", ints.OrderBy(i => i).ToArray());
    
    
        private static IEnumerable<int> Expand(string str)
        {
            if (str.Contains('-'))
            {
                var range = str.Split('-');
                int begin = int.Parse(range[0]);
                int end = int.Parse(range[1]);
                for (int i = begin; i <= end; i++)
                    yield return i;
            }
            else
                yield return int.Parse(str);
        }
    

    Of course you might want to add some error checking, but I’ll leave that up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string that look like Array that fetched from other webservice like this
We have data that might look something like: A-10001 A-10002 A-10003 B-10001 B-10002 B-10003
I have a String that looks like this String = Förpackning Flaska (375 ml)
I have a string that looks like this: <name>-<gender>-<age>.jpg I want to be very
I have an input string I'm trying to parse. It might look like either
We store documents that look something like this: { id: dQesbpxeQniUWXpsnjPQ, title: Golf in
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have 4 string variables name, quest, favorite_color, speed that might be empty. I
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
I am wondering if something like this could be done(of course what I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.