Code Logo

Ascending Manifest

Published at05 Jan 2026
Topological Sort Easy 25 views
Like30

You are given a list of shipment values and need to produce the same list in ascending order, from the smallest value to the largest.

This is not a filtering problem. You do not remove repeated numbers, and you do not treat negative values specially. Every original number must appear in the returned list exactly as many times as it appeared in the input — only the arrangement changes.

For example, [4,-1,4,3] should become [-1,3,4,4], keeping both copies of 4. If the input is [7,2,5,2], the answer is [2,2,5,7], which keeps both copies of 2. An empty input produces an empty output.

Sorting a collection is one of the most fundamental operations in programming. It is the foundation for binary search, deduplication, merging, and nearly every data-analysis workflow. Most languages provide a built-in sort that runs in O(n log n) time, which is what you should use here.

A subtle detail in some languages is that the default sort may compare values as strings. JavaScript's sort() without a comparator, for example, would order [10, 2, 1] as [1, 10, 2]. To get true numeric order, you must pass a numeric comparator.

Edge cases include an empty list (return an empty list), a single value (return it unchanged), duplicates (each copy is preserved), and negative values (they sort before positive values, with more negative numbers coming first).

Example Input & Output

Example 1
Input
nums = [4,-1,4,3]
Output
[-1,3,4,4]
Explanation

Negative and positive labels appear in ascending order, with repeated values preserved.

Example 2
Input
nums = []
Output
[]
Explanation

An empty shipment produces an empty manifest.

Example 3
Input
nums = [7,2,5,2]
Output
[2,2,5,7]
Explanation

The returned manifest lists the labels from smallest to largest while keeping both copies of 2.

Algorithm Flow

Recommendation Algorithm Flow for Ascending Manifest

Solution Approach

Sort the array in ascending numeric order using a built-in sort, making sure the comparator is numeric rather than string-based.

function ascending_manifest(nums) {
  return nums.slice().sort(function(a, b) { return a - b; });
}

Creating a copy with slice() avoids mutating the input. The comparator a - b produces true numeric ordering: a negative result means a comes first, zero means equal, and a positive result means b comes first. This handles negative numbers, zero, and duplicates correctly.

In languages with a native numeric sort, you can call it directly, such as sorted(nums) in Python or nums.sort() on a Rust Vec of integers. The key is that the ordering is by numeric value, not by string representation.

Time complexity is O(n log n), space complexity is O(n) for the copy.

Best Answers

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