Code Logo

Harbor Cargo Lineup

Published at05 Jan 2026
Easy 1 views
Like24

In Harbor Cargo Lineup, each entry is a string with three parts separated by #, such as "PierA#cocoa#2". You can read that as berth # cargo tag # sequence.

The task is to sort the entries using those parts. The cargo tag is the main sorting key, and it should be compared alphabetically without caring about letter case. If two entries have the same cargo tag, compare their sequence numbers as numbers, not as text. If there is still a tie after that, use the berth name as the final tie-breaker.

For example, ["Dock9#tea#12","Dock2#linen#3","Dock5#tea#1"] should become ["Dock2#linen#3","Dock5#tea#1","Dock9#tea#12"]. The linen cargo comes before tea, and inside the tea group the entry with sequence 1 comes before sequence 12. Another example is ["A#X#1","A#X#10","A#X#2"], which should become ["A#X#1","A#X#2","A#X#10"].

So the job is to split each manifest string into meaningful parts, compare those parts in the right priority order, and return the original strings in that sorted order.

Example Input & Output

Example 1
Input
entries = ["A#X#1","A#X#10","A#X#2"]
Output
["A#X#1","A#X#2","A#X#10"]
Explanation

The sequence field is compared numerically, so 2 comes before 10.

Example 2
Input
entries = ["PierA#cocoa#2","PierB#algae#7","PierB#algae#5","PierA#cocoa#1"]
Output
["PierB#algae#5","PierB#algae#7","PierA#cocoa#1","PierA#cocoa#2"]
Explanation

Cargo order puts algae before cocoa, and sequence order sorts entries inside each cargo group.

Example 3
Input
entries = []
Output
[]
Explanation

With no manifest lines to organize, the result stays empty.

Algorithm Flow

Recommendation Algorithm Flow for Harbor Cargo Lineup
Recommendation Algorithm Flow for Harbor Cargo Lineup

Solution Approach

A solid way to solve this problem is to sort the manifest strings with a custom comparator that understands the three parts inside each entry. Every string has the shape berth#cargo#sequence, so the main work is parsing those pieces and then comparing them in the correct priority order.

The first comparison key is the cargo tag. The tests show that this comparison should ignore case, so "Amber" and "amber" should be treated as the same cargo word for ordering purposes. If two entries have different cargo tags, that comparison alone decides their order. If the cargo tags are the same, the second key is the sequence number, which must be compared numerically. This matters because string comparison would put "10" before "2", which would be wrong here. If the cargo tag and numeric sequence are both tied, the berth name can break the tie so the result stays deterministic.

In JavaScript, the sorting logic can look like this:

entries.sort((a, b) => {
  const [berthA, cargoA, seqA] = a.split('#');
  const [berthB, cargoB, seqB] = b.split('#');

  const cargoCmp = cargoA.toLowerCase().localeCompare(cargoB.toLowerCase());
  if (cargoCmp !== 0) return cargoCmp;

  const seqCmp = Number(seqA) - Number(seqB);
  if (seqCmp !== 0) return seqCmp;

  return berthA.toLowerCase().localeCompare(berthB.toLowerCase());
});
return entries;

This matches the judge examples directly. In the algae versus cocoa example, the algae entries come first because the cargo word sorts earlier. Inside that algae group, sequence 5 comes before 7. In ["A#X#1", "A#X#10", "A#X#2"], converting the sequence to a number is what produces 1, 2, 10 instead of the incorrect text order 1, 10, 2.

The time complexity is O(n log n) because sorting dominates the work. The extra space depends on the language's sort implementation. The important strategy is: parse each string, compare cargo case-insensitively, compare sequence numerically, use berth as the last tie-breaker, and return the original strings in that sorted order.

Best Answers

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