Problem Statement
You are given a String[], items, each element of which represents a single mail order item and is formatted as "<description> <price> <shipping>" (quotes for clarity only). <description> contains a textual description of the item and consists of only lowercase letters ('a'-'z') and spaces (but no leading or trailing spaces). <price> represents the item's base price and <shipping> represents the item's shipping cost. Both <price> and <shipping> are formatted as "$D.CC" (quotes for clarity only), where D is the number of dollars (an integer between 0 and 1000, inclusive, with no extra leading zeros) and CC represents the number of cents (an integer between 00 and 99, inclusive, with exactly 2 digits).
Sort items in ascending order by total price, where the total price of an item is equal to the base price plus the shipping cost. The constraints will guarantee that no two items will have the same total price. The return value is a String[] whose elements are formatted exactly the same as items.
Definition
- Class:
- TruePriceOrder
- Method:
- sort
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] sort(String[] items)
- (be sure your method is public)
Constraints
- items will contain between 1 and 50 elements, inclusive.
- Each element of items will be formatted as described in the statement.
- Each <description> will be between 1 and 30 characters in length, inclusive.
- No two elements of items will have the same (<price> + <shipping>).
Examples
{"toaster oven $48.88 $15.00", "dvd $9.00 $2.00", "gift certificate $10.00 $0.00", "pants $29.75 $4.99", "heavy thing $0.00 $99.99"}Returns: {"gift certificate $10.00 $0.00", "dvd $9.00 $2.00", "pants $29.75 $4.99", "toaster oven $48.88 $15.00", "heavy thing $0.00 $99.99" }"heavy thing" has the lowest price, but once the shipping cost is factored in, it becomes the most expensive item. Similarly, "dvd" has a lower price than "gift certificate", but "gift certificate" ends up being cheaper because of its free shipping.{"same thing $4.99 $5.00", "same thing $6.99 $2.99", "same thing $9.95 $0.00"}Returns: {"same thing $9.95 $0.00", "same thing $6.99 $2.99", "same thing $4.99 $5.00" }