Code Logo

Midnight Vendor Queue Order

Published at05 Jan 2026
Easy 3 views
Like17

In Midnight Vendor Queue Order, each item in the input is a two-part record, such as ["A", 10]. The first part is a string label, and the second part is a number. Your task is to sort the full list using both pieces of information.

The order rule is straightforward: compare the string labels first. Entries with smaller labels come earlier. If two entries have the same label, then compare their numbers and place the smaller number first. Nothing should be removed, merged, or reformatted. You just return the same records in the right order.

For example, if the input is orders = [["A",10],["B",5],["A",2]], the answer should be [["A",2],["A",10],["B",5]]. Both "A" entries come before the "B" entry, and inside the "A" group the smaller number 2 comes before 10. If the input is orders = [["M",1],["Z",10],["M",5]], the result is [["M",1],["M",5],["Z",10]] for the same reason.

So the task is to sort the records by a compound key: label ascending first, then number ascending.

Example Input & Output

Example 1
Input
orders = [["A",10],["B",5],["A",2]]
Output
[["A",2],["A",10],["B",5]]
Explanation

Labels are compared first, so both A entries come before B, and their numbers are sorted inside the A group.

Example 2
Input
orders = [["X",100],["X",10]]
Output
[["X",10],["X",100]]
Explanation

When labels match, the smaller number should come first.

Example 3
Input
orders = [["M",1],["Z",10],["M",5]]
Output
[["M",1],["M",5],["Z",10]]
Explanation

The two M entries stay together and are ordered by number before the Z entry.

Algorithm Flow

Recommendation Algorithm Flow for Midnight Vendor Queue Order
Recommendation Algorithm Flow for Midnight Vendor Queue Order

Solution Approach

A good way to solve this problem is to sort the list with a custom comparator that looks at both parts of each record. Since each item is a pair like [label, number], the comparator should first decide order by the string label. If the labels are different, that answer is enough. If the labels are the same, then it should compare the numeric part.

That means the rule is really a two-level sort. First compare the label values in ascending order. Then, only when the labels match, compare the numbers in ascending order. You can think of the label as the primary key and the number as the secondary key.

In JavaScript, the main idea looks like this:

orders.sort((a, b) => {
  if (a[0] !== b[0]) return a[0].localeCompare(b[0]);
  return a[1] - b[1];
});
return orders;

The first line inside the comparator handles the label rule. A record with label "A" should come before one with label "B". The second line handles ties inside the same label group. So ["A", 2] comes before ["A", 10].

This directly matches the examples. In [["A",10],["B",5],["A",2]], both "A" entries are grouped together before "B", and then the numeric values decide their internal order. In [["M",1],["Z",10],["M",5]], the two "M" records stay together and are ordered as 1 then 5, while "Z" stays behind them because the label is larger. Empty input stays empty because there is nothing to sort. A single entry also stays unchanged.

The nice thing about this approach is that it keeps the problem focused on the exact sorting rule instead of adding unnecessary parsing or helper structures. You do not need a map, prefix array, or nested loops. You only need a comparison function that expresses the intended order clearly. That makes the code short, but still very readable.

The time complexity is O(n log n), which is standard for sorting. Extra space depends on the language and sorting implementation, but the logic itself is simple and compact. If you want to avoid mutating the original list, you can copy the input before sorting. Otherwise, sorting in place is fine for this kind of problem.

So the full strategy is: sort by the first field as text, break ties using the second field as a number, and return the reordered list of pairs.

Best Answers

java
import java.util.*;
class Solution {
    public int[] sort_queue(int[] nums) {
        int[] res = nums.clone();
        Arrays.sort(res);
        return res;
    }
}