Code Logo

Sort Items by Custom Criteria

Published at13 Jan 2026
Medium 7 views
Like12

You are given a list of items and need to sort them using a custom rule instead of relying on plain default ordering.

The actual test data shows that each item is a tuple like (name, score), and the list should be sorted by the numeric score in ascending order. So if the input is [("apple", 5), ("banana", 2), ("cherry", 8)], the correct result is [("banana", 2), ("apple", 5), ("cherry", 8)].

This means the string part is not the main sorting key here. The number in position 1 is what decides the order. If several items have the same score, Python's normal stable sort behavior keeps their relative order unless you add another rule.

So the task is to sort the input items by a custom key function that looks at the score part of each tuple.

Example Input & Output

Example 1
Input
items = [("a", 1)]
Output
[("a", 1)]
Explanation

A single tuple is already in sorted order.

Example 2
Input
items = [("apple", 5), ("banana", 2), ("cherry", 8)]
Output
[("banana", 2), ("apple", 5), ("cherry", 8)]
Explanation

The tuples are sorted by their numeric second field in ascending order.

Example 3
Input
items = [("x", 10), ("y", 5), ("z", 7)]
Output
[("y", 5), ("z", 7), ("x", 10)]
Explanation

Lower scores come first, regardless of the string label.

Algorithm Flow

Recommendation Algorithm Flow for Sort Items by Custom Criteria
Recommendation Algorithm Flow for Sort Items by Custom Criteria

Solution Approach

This problem is really about Python's key-based sorting. When the items themselves are not meant to be compared by their full default value, the clean solution is to tell sorted() exactly which part of each item should drive the ordering.

Here the inputs are tuples like (name, score), and the tests show that the list should be ordered by the numeric score. That means the sorting key should read the value at index 1 from each tuple.

A direct solution is:

return sorted(items, key=lambda item: item[1])

The lambda here is the custom criteria. For every tuple, it extracts the score, and sorted() uses that extracted score to decide the final order.

This is better than trying to manually compare tuples in loops because Python's built-in sort is already efficient and stable. Stable means that if two items have the same score, they keep their original relative order from the input unless another key is introduced.

Another useful point is that the code stays flexible. If the business rule changed later and you needed descending order, or a second tiebreaker by name, you could adjust the key rather than rewriting the whole algorithm. For example, you might sort by (item[1], item[0]) for a score-first, name-second rule.

You could also use the list method items.sort(key=...) if you want to sort in place. The difference is that sorted() returns a new list and leaves the original input untouched, while list.sort() mutates the original list. In many interview-style or platform-style problems, returning a fresh sorted list is the safer and clearer choice unless the statement explicitly asks for in-place modification.

The time complexity is O(n log n), which is standard for comparison-based sorting. The important lesson is not just "sort the list," but "sort the list by the right field." The custom key is what makes the solution match the real problem instead of accidentally sorting by the wrong part of each item.

Best Answers

python - Approach 1
def sort_and_filter(items):
    get_value = lambda x: x[1]
    return sorted(items, key=get_value)