-3

Can anyone please help in build a logic to sort list of string which contains ranged number items.

Ex: List myList = new List() { "1", "3-6", "2", "8-10","7", "11", "13-18", "12"};

Sorted Output List: 1, 2, 3-6, 7, 8-10, 11, 12, 13-18

Note: List contains only positive numbers.

Thanks, Arjun

Sandeep S
  • 1
  • 1
  • 2
    Have you tried [List.Sort](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort?view=net-5.0)? Or [Enumerable.OrderBy](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=net-5.0)? – SomeBody Jan 08 '21 at 09:29
  • 2
    what if you have a value in between a range like 5 here "1", "3-6", "5", "2", "8-10","7", "11", "13-18", "12" – Mihir Dave Jan 08 '21 at 09:29
  • 3
    we´re not here to do your job, which is **thinking, trying out, thinking again**. However we are happy to help you with a specific problem with your code. Provide what you´ve tried and where you got problems. – MakePeaceGreatAgain Jan 08 '21 at 09:41
  • @MihirDave Its not a valid case as number 5 comes within the range of 3-6. – Sandeep S Jan 08 '21 at 09:48
  • 1
    if your input does not contain any numbers within a range, you can simply extract the lower bound of every range and use that for sorting. – MakePeaceGreatAgain Jan 08 '21 at 09:51
  • Thats information will be better fit in your question. Use the [edit] button to add the rules about those edge case. other edge case Are the range always ordered {1-3, 12-4, 13}? What is your issue? Are you able to order a list of int? Are you able to transphom a string into a int? – Drag and Drop Jan 08 '21 at 09:53
  • 1
    I will really recommend using [ask], and [mre] as guideline for writing a question or solving an issue. I'm pretty sure that a clear description of what you want will be almost compilable code. – Drag and Drop Jan 08 '21 at 09:55
  • And negative value? ranging from -10 to -1? `"-10--1"`? – Drag and Drop Jan 08 '21 at 10:03
  • No negative number allowed in list – Sandeep S Jan 08 '21 at 11:08

1 Answers1

-1

The basics step are:

Now here is a way to do it:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static void Main(string[] args)
    {
        var myList = new List<string> { "1", "3-6", "2", "8-10", "7", "11", "13-18", "12" };
        var ranges = myList.Select(x=> new Range(x)).ToList();
        ranges.Sort();//Order yourself!

        //Display
        foreach (var range in ranges) Console.WriteLine(range);

        //One line with comma separator
        Console.WriteLine(string.Join(", ", ranges));
    }

    public class Range : IComparable<Range>
    {
        // Simple ctor, with Customisable separator in case of Negative Value. 
        public Range(string input, char separator= '-') {
            if (!input.Contains(separator))// Is it a range?
            {//It's not then
                if (int.TryParse(input, out int value))// Lets try to convert it to an integer
                {// it's a int then
                    Min = value; Max = value; // both min and max 
                }
                else
                {// It's not a range and it's not an int! Scream!
                    throw new ArgumentException(input + "  is not a valid input !");
                }
            }
            else 
            {//It's a range
                var parts = input.Split(new char[] { separator }, 2);// split in 2 on the separator
                if (int.TryParse(parts[0], out int part1) && int.TryParse(parts[1], out int part2))// Both part are int? 
                {                        
                    Min = Math.Min(part1, part2); // min is the min and max the max.
                    Max = Math.Max(part1, part2); // It's easier to compute min/max than try to fix a bad input.
                }
                else {// It's look like a range but those are not int! Scream!
                    throw new ArgumentException(input + "  is not a valid input !" );
                }
            }
        }
        public int Min { get; set; }
        public int Max { get; set; }

        // Comparing bound together. {1-1} < {1-2} < {1-6} < {2-2}; 
        public int CompareTo(Range obj)
        {
            var mincomparer = this.Min.CompareTo(obj.Min);
            var maxcomparer = this.Max.CompareTo(obj.Max);
            return mincomparer == 0 ? maxcomparer : mincomparer;
        }

        public override string ToString()
        {
            return Min == Max ? Min.ToString() : Min + "-" + Max;
        }
    }
}

LiveDemo

Self
  • 349
  • 1
  • 8