Choose the right sorting approach using Python's built-in Timsort, heapq, or custom strategies. Use when sorting arrays, finding top-k elements, or ordering by multiple keys.
Scanned 9/7/2026
Install to Claude Code
npx -y skills add knoopx/pi --skill sorting-choice --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Sorting Choice?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/knoopx-sorting-choice)More formats (shields.io, HTML) on the badges page.
---
name: sorting-choice
description: "Choose the right sorting approach using Python's built-in Timsort, heapq, or custom strategies. Use when sorting arrays, finding top-k elements, or ordering by multiple keys."
topic: Sorting
token_cost: 90
related: [binary-search, two-pointers, hash-vs-tree]
keywords:
[
sort,
order,
rank,
largest,
smallest,
kth,
median,
arrange,
compare,
stable,
priority,
heap,
nlargest,
nsmallest,
key,
reverse,
sorted,
]
---
## When to use
Python's built-in sorted()/list.sort() is Timsort — O(n log n), stable, and almost always the right choice.
## Rules
- Use key= for custom ordering
- For top-k elements, use heapq.nlargest/nsmallest (O(n log k)) instead of full sort
- For finding just the kth element, consider quickselect or statistics.median
- Counting sort / radix sort help only when values are bounded integers
- When the problem says "sort by X then by Y," use a tuple key: key=lambda x: (x.a, x.b)
- For reverse on one field only, negate it or use functools.cmp_to_key
- ALWAYS prefer built-in sort — it's optimized and stable
- NEVER implement your own sort algorithm unless the problem requires it
## Complexity
Timsort: O(n log n). heapq.nlargest/nsmallest: O(n log k).
## Example
"Top 3 scores" → `heapq.nlargest(3, scores)` in O(n log 3). "Sort by name then age" → `sorted(items, key=lambda x: (x.name, x.age))`.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!
Use this skill when developing or maintaining browser extension code in the `browser/` directory, including Chrome/Firefox/Edge compatibility, content scripts, background scripts, or i18n updates.
SEO optimization with keyword analysis, readability assessment, technical validation, content quality. Use for search rankings, blog posts, content audits, or encountering keyword density, readability scores, meta tags, schema markup errors.
Official Google SEO guide covering search optimization, best practices, Search Console, crawling, indexing, and improving website search visibility based on official Google documentation
Python backend development expertise for FastAPI, security patterns, database operations, Upstash integrations, and code quality. Use when: (1) Building REST APIs with FastAPI, (2) Implementing JWT/OAuth2 authentication, (3) Setting up SQLAlchemy/async databases, (4) Integrating Redis/Upstash caching, (5) Refactoring AI-generated Python code (deslopification), (6) Designing API patterns, or (7) Optimizing backend performance.
Build a full-stack TanStack Start app on Cloudflare Workers from scratch — SSR, file-based routing, server functions, D1+Drizzle, better-auth, Tailwind v4+shadcn/ui. Use whenever the user mentions TanStack Start, asks to scaffold a full-stack Cloudflare app with SSR, wants an SSR dashboard, or asks for a React 19 + Cloudflare Workers app with file-based routing and server functions — even if they don't name TanStack Start specifically. No template repo — Claude generates every file fresh per ...