Dsa
Sorting
Medium
Sort Product Reviews
You are given a list of product reviews, where each review consists of a rating (1 to 5) and a review text separated by a comma. Your task is to sort the reviews based on the rating in descending order. In cases where two reviews have the same rating, sort them lexicographically by their review text in ascending order. Finally, print the sorted reviews in the original format.
Input format:
- Each review is provided on a new line.
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of reviews.
- The following n lines each contain a review.
Output format:
- Print the sorted reviews, one per line.
Example:
Input:
3
5,Great product
3,Okay product
5,Amazing value
Output:
5,Great product
5,Amazing value
3,Okay product
Key concepts
sortinglist manipulationstring parsing
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now