In Gallery Queue Sort, you are given an array of integers and asked to return the same values sorted from smallest to largest.
The key point is that the values themselves do not change. You are only rearranging them. If a number appears twice, both copies should still be in the answer. If the array includes negative values or zero, those should also be placed using normal numeric order. So a negative number belongs before zero, and zero belongs before larger positive numbers.
For example, nums = [14,5,12,5] should become [5,5,12,14]. Both copies of 5 are kept, just moved into the correct positions. If nums = [0,-3,7], the result should be [-3,0,7] because -3 is the smallest value in the list.
An empty array stays empty, and a one-element array stays the same. So the task is simply to sort the full list in ascending numeric order and return that sorted array.
Example Input & Output
The values are reordered from low to high, and both copies of 5 stay in the result.
Negative values still follow ordinary numeric order, so -3 comes first.
A single badge remains unchanged because it already satisfies the order.
Algorithm Flow

Solution Approach
A direct numeric sort is the right approach for this problem. The task is not asking you to count anything, detect a pattern, or check whether the array is already sorted. It simply wants the values returned in ascending order, so the cleanest answer is to sort them and return the result.
The one technical detail that matters is that the sort must be numeric. In some languages, especially JavaScript, using the default sort without a comparator can accidentally compare numbers as strings. That gives wrong results like placing 12 before 5 because the character 1 comes before 5. So even though the problem is easy, it is still worth being explicit about how comparison works.
In JavaScript, a reliable version looks like this:
The spread operator creates a copy, which helps if you do not want to mutate the original input array. The comparator a - b tells the sort to arrange values by their real numeric size. If a is smaller than b, the comparator returns a negative number, so a stays earlier in the sorted order.
This naturally handles duplicates. If the input contains two 5 values, both remain in the final array because sorting changes order but does not remove elements. Negative values also work with no special logic. A number like -3 compares as smaller than 0 and 7, so it moves to the front automatically.
Edge cases are straightforward too. An empty array returns an empty array because there is nothing to sort. A one-value array is already sorted. Mixed arrays with negatives, positives, and repeated numbers all follow the same comparator rule and need no extra branch.
The time complexity is O(n log n), which is standard for comparison sorting. Extra space depends on the language and whether you sort in place or on a copy. So the full strategy is: take the queue numbers, apply an ascending numeric sort with a proper comparator, and return the sorted array with every original value still present.
Best Answers
import java.util.*;
class Solution {
public int[] sort_queue(int[] nums) {
int[] res = nums.clone();
Arrays.sort(res);
return res;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
