Kobek
Kobek

Reputation: 1211

C# converting a stack to an array and sorting it

I recently got into C# and I was trying to learn a bit about Stacks. I wanted to try and sort a stack, by first converting it to an array but I got a weird error.

Here is my code:

using System;
using System.Collections.Generic;

class Program
{
    public static Stack<int> numbers = new Stack<int>();
    static void Main(string[] args)
    {
        string[] input = Console.ReadLine().Split(' ');

        int n = int.Parse(input[0]);
        int s = int.Parse(input[1]);
        int x = int.Parse(input[2]);

        input = Console.ReadLine().Split(' ');
        for (int i = 0; i < n; i++)
        {
            numbers.Push(int.Parse(input[i]));
        }

        for (int i = 0; i < s; i++)
        {
            numbers.Pop();
        }

        if (numbers.Count == 0)
            Console.WriteLine(0);
        else if (numbers.Contains(x))
            Console.WriteLine("true");
        else
            Console.WriteLine(Array.Sort(numbers.ToArray()));


    }
}

My issue is on the last else part of my code (last line) :

Argument 1: cannot convert from 'void' to 'bool'

I was wondering why this is happening, when Array.Sort(), requires an Array as a parameter, and I pass number.ToArray(), which should return a new Array out of the numbers Stack.

Upvotes: 0

Views: 3506

Answers (7)

Kobek
Kobek

Reputation: 1211

Thank you to everyone for the responses. I am coming from Java, where you could directly print a collection, that's why I tried to do it like that.

Upvotes: 0

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

First convert as Array Object. then sort your array and then you should use

Console.WriteLine(array) to print your Array.

int[] numberArray = numbers.ToArray(); //change to Array.
  numberArray.sort();                   // it will sort your Array.
  Console.WriteLine(numberArray);   //display the sorted Array.

instead of

 Console.WriteLine(Array.Sort(numbers.ToArray())); // here whatever Sort() method return will be printed as Output but infact Sort() returns void.

So Compile time your get Error: cannot convert from 'void' to 'bool'

Upvotes: 1

Hari Prasad
Hari Prasad

Reputation: 16956

Array.Sort is a void method, you've to separate Sort and writing (to console) in two different statements.

I would suggest using OrderBy linq extension and string.Join together to have one liner display.

Console.WriteLine(string.Join(",", numbers.OrderBy(x=>x));

Upvotes: 2

Orel Eraki
Orel Eraki

Reputation: 12196

Error appears due the fact Array.Sort returns void and not the expected array.

Additionally, Console.WriteLine gets inputs and prints it to Console, it can not get array and print that array, it will only print you the actual array address.

In order to print array(or any collection) you should use string.Join which takes a collection and returns a string separated by delimiter.

Pretty straightforward using System.Linq.OrderBy

numbers.OrderBy(num => num).ToArray();

Example:

public static void Main()
{
    var numbers = new Stack<int>();
    numbers.Push(4);
    numbers.Push(1);
    numbers.Push(2);

    var numbersSorted = numbers.OrderBy(num => num).ToArray();
    Console.WriteLine(string.Join(", ", numbersSorted));
}

Output: 1, 2, 4

DotNetFiddle

Upvotes: 7

Nasreddine
Nasreddine

Reputation: 37838

The error message is quite clear. In this line:

Console.WriteLine(Array.Sort(numbers.ToArray()));

The Array.Sort doesn't return anything (void) but you're passing to Console.WriteLine which expects a parameter.

You should replace the last else with this:

var numbersArray = numbers.ToArray();
Array.Sort(numbersArray);

Console.WriteLine(string.Join(",", numbers));

Upvotes: 1

RobPethi
RobPethi

Reputation: 571

You need to assign an array variable before sorting, which you can then writeline.

    else
    {
        var array = numbers.ToArray();
        Array.Sort(array);
        Console.WriteLine(array);

    }

Upvotes: 1

PhillipH
PhillipH

Reputation: 6222

Array.Sort carries out an in-place sort on the array. It does not return a new sorted array. It is therefore a Void function.

Modify your code;

int[] numberArray = numbers.ToArray();
numberArray.Sort();
Console.WriteLine(numberArray);

Upvotes: 2

Related Questions