Sort string of comma separated numbers using C# and VB.Net in Console Application

Last Reply 4 months ago By dharmendr

Posted 4 months ago
Write a C# console app called NumberParser which accepts a comma delimited list of integers as the first parameter, and a file format as the second parameter. The app would be invoked via the command line like this - NumberParser 4,5,1,9,10,58,34,12,0 XML. 

The app should parse the first parameter into an array of integers, sort it into descending order and then persist it in the specified format. The app should support text files, XML and JSON formats.

As well as delivering the requirements, the solution should demonstrate how to use an interface and how to use the factory pattern.

NumberParser 4,5,1,9,10,58,34,12,0  Txt.

Read the numbers and sort it in descending along with the format.

output: 58 34 12,10,9,5,4,1,0 Txt

Same way for XML

NumberParser 4,5,1,9,10,58,34,12,0 XML. 

output: 58 34 12,10,9,5,4,1,0 XML

Same way for JSON

NumberParser 4,5,1,9,10,58,34,12,0 JSON.

output: 58 34 12,10,9,5,4,1,0 JSON

Posted 4 months ago

Hi chetan,

Check this example. Now please take its reference and correct your code.

Code

C#

class Program
{
    static void Main(string[] args)
    {
        string numberFile;
        Console.Write("Enter Number and File:\t");
        numberFile = Console.ReadLine();
        string numbers = numberFile.Split(' ')[0];
        string file = numberFile.Split(' ')[numberFile.Split(' ').Length - 1];
        Console.Write(SortNumberArrayDesc(numbers) + " " + file);
        Console.ReadLine();
    }


    private static string SortNumberArrayDesc(string number)
    {
        string[] intArray = number.Split(',');
        int temp = 0;
        for (int i = 0; i <= intArray.Length - 1; i++)
        {
            for (int j = i + 1; j < intArray.Length; j++)
            {
                if (Convert.ToInt32(intArray[i]) < Convert.ToInt32(intArray[j]))
                {
                    temp = Convert.ToInt32(intArray[i]);
                    intArray[i] = intArray[j];
                    intArray[j] = temp.ToString();
                }
            }
        }

        return string.Join(",", intArray);
    }
}

VB.Net

Module Program
    Sub Main(args As String())
        Dim numberFile As String
        Console.Write("Enter Number and File:" & vbTab)
        numberFile = Console.ReadLine()
        Dim numbers As String = numberFile.Split(" "c)(0)
        Dim file As String = numberFile.Split(" "c)(numberFile.Split(" "c).Length - 1)
        Console.Write(SortNumberArrayDesc(numbers) & " " & file)
        Console.ReadLine()
    End Sub

    Private Function SortNumberArrayDesc(ByVal number As String) As String
        Dim intArray As String() = number.Split(","c)
        Dim temp As Integer = 0
        For i As Integer = 0 To intArray.Length - 1
            For j As Integer = i + 1 To intArray.Length - 1
                If Convert.ToInt32(intArray(i)) < Convert.ToInt32(intArray(j)) Then
                    temp = Convert.ToInt32(intArray(i))
                    intArray(i) = intArray(j)
                    intArray(j) = temp.ToString()
                End If
            Next
        Next

        Return String.Join(",", intArray)
    End Function
End Module

Screenshot