简体   繁体   中英

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.

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

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);

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));

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));

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);

    }

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM