Challenge: Sort Values in a Stack

In this lesson, we will learn how to sort elements using a stack.

Problem Statement

You have to implement myStack sortStack(myStack stack, int size) function which will take a stack and sort all its elements in ascending order.


A stack of integers.


Returns the stack with all its elements sorted.

Sample Input

//input stack where 23 is the top
23, 60, 12, 42, 4, 97, 2

Sample Output

//resultant stack where 2 is the top
 2, 4, 12, 23, 42, 60, 97

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.