diff --git a/pypistats/__init__.py b/pypistats/__init__.py
index 21c78e2..719f31a 100644
--- a/pypistats/__init__.py
+++ b/pypistats/__init__.py
@@ -15,7 +15,13 @@ BASE_URL = "https://pypistats.org/api/"
 
 
 def pypi_stats_api(
-    endpoint, params=None, output="table", start_date=None, end_date=None, total=True
+    endpoint,
+    params=None,
+    output="table",
+    start_date=None,
+    end_date=None,
+    sort=True,
+    total=True,
 ):
     """Call the API and return JSON"""
     if params:
@@ -40,6 +46,10 @@ def pypi_stats_api(
     if output == "json":
         return res
 
+    # Only sort for table
+    if sort:
+        res["data"] = _sort(res["data"])
+
     data = res["data"]
     return _tabulate(data)
 
@@ -63,6 +73,17 @@ def _filter(data, start_date=None, end_date=None):
     return data
 
 
+def _sort(data):
+    """Sort by downloads"""
+
+    # Only sum lists of dicts, not a single dict
+    if isinstance(data, dict):
+        return data
+
+    data = sorted(data, key=lambda k: k["downloads"], reverse=True)
+    return data
+
+
 def _total(data):
     """Sum all downloads per category, regardless of date"""
 

Sort table by downloads

| category | downloads |
|----------|----------:|
|      2.6 |      1968 |
|      2.7 |   1594594 |
|      3.1 |         3 |
|      3.2 |       244 |
|      3.3 |      1824 |

->


| category | downloads |
|----------|----------:|
|      2.7 |   1,594,594 |
|      2.6 |      1,968 |
|      3.3 |      1,824 |
|      3.2 |       244 |
|      3.1 |         3 |

diff --git a/tests/test_pypistats.py b/tests/test_pypistats.py
index c925377..cad9a90 100644
--- a/tests/test_pypistats.py
+++ b/tests/test_pypistats.py
@@ -7,6 +7,19 @@ import unittest
 
 import pypistats
 
+SAMPLE_DATA = [
+    {"category": "2.6", "date": "2018-08-15", "downloads": 51},
+    {"category": "2.7", "date": "2018-08-15", "downloads": 63749},
+    {"category": "3.2", "date": "2018-08-15", "downloads": 2},
+    {"category": "3.3", "date": "2018-08-15", "downloads": 40},
+    {"category": "3.4", "date": "2018-08-15", "downloads": 6095},
+    {"category": "3.5", "date": "2018-08-15", "downloads": 20358},
+    {"category": "3.6", "date": "2018-08-15", "downloads": 35274},
+    {"category": "3.7", "date": "2018-08-15", "downloads": 6595},
+    {"category": "3.8", "date": "2018-08-15", "downloads": 3},
+    {"category": "null", "date": "2018-08-15", "downloads": 1019},
+]
+
 
 class TestPypiStats(unittest.TestCase):
     def test__filter_no_filters_no_change(self):
@@ -115,18 +128,7 @@ class TestPypiStats(unittest.TestCase):
 
     def test__tabulate(self):
         # Arrange
-        data = [
-            {"category": "2.6", "date": "2018-08-15", "downloads": 51},
-            {"category": "2.7", "date": "2018-08-15", "downloads": 63749},
-            {"category": "3.2", "date": "2018-08-15", "downloads": 2},
-            {"category": "3.3", "date": "2018-08-15", "downloads": 40},
-            {"category": "3.4", "date": "2018-08-15", "downloads": 6095},
-            {"category": "3.5", "date": "2018-08-15", "downloads": 20358},
-            {"category": "3.6", "date": "2018-08-15", "downloads": 35274},
-            {"category": "3.7", "date": "2018-08-15", "downloads": 6595},
-            {"category": "3.8", "date": "2018-08-15", "downloads": 3},
-            {"category": "null", "date": "2018-08-15", "downloads": 1019},
-        ]
+        data = SAMPLE_DATA
         expected_output = """
 | category |    date    | downloads |
 |----------|------------|----------:|
@@ -148,6 +150,38 @@ class TestPypiStats(unittest.TestCase):
         # Assert
         self.assertEqual(output.strip(), expected_output.strip())
 
+    def test__sort(self):
+        # Arrange
+        data = SAMPLE_DATA
+        expected_output = [
+            {"category": "2.7", "date": "2018-08-15", "downloads": 63749},
+            {"category": "3.6", "date": "2018-08-15", "downloads": 35274},
+            {"category": "3.5", "date": "2018-08-15", "downloads": 20358},
+            {"category": "3.7", "date": "2018-08-15", "downloads": 6595},
+            {"category": "3.4", "date": "2018-08-15", "downloads": 6095},
+            {"category": "null", "date": "2018-08-15", "downloads": 1019},
+            {"category": "2.6", "date": "2018-08-15", "downloads": 51},
+            {"category": "3.3", "date": "2018-08-15", "downloads": 40},
+            {"category": "3.8", "date": "2018-08-15", "downloads": 3},
+            {"category": "3.2", "date": "2018-08-15", "downloads": 2},
+        ]
+
+        # Act
+        output = pypistats._sort(data)
+
+        # Assert
+        self.assertEqual(output, expected_output)
+
+    def test__sort_recent(self):
+        # Arrange
+        data = {"last_day": 123002, "last_month": 3254221, "last_week": 761649}
+
+        # Act
+        output = pypistats._sort(data)
+
+        # Assert
+        self.assertEqual(output, data)
+
     def test__total(self):
         # Arrange
         from data.python_minor import data

pip install -e .
parse_log_pytest
pytest
pytest pytest-cov
apt-get update apt-get install -y gcc
3.9
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi==2025.1.31
chardet==5.2.0
charset-normalizer==3.4.1
coverage==7.8.0
DataProperty==1.1.0
exceptiongroup @ file:///croot/exceptiongroup_1706031385326/work
idna==3.10
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
mbstrdecoder==1.1.4
packaging @ file:///croot/packaging_1734472117206/work
pathvalidate==3.2.3
pluggy @ file:///croot/pluggy_1733169602837/work
-e git+https://github.com/hugovk/pypistats.git@b9514c436ebde9441532e6875f196d5a2be55fb7#egg=pypistats
pytablewriter==1.2.1
pytest @ file:///croot/pytest_1738938843180/work
pytest-cov==6.0.0
python-dateutil==2.9.0.post0
pytz==2025.2
requests==2.32.3
six==1.17.0
tabledata==1.3.4
tcolorpy==0.1.7
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
typepy==1.3.4
urllib3==2.3.0

name: pypistats
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - exceptiongroup=1.2.0=py39h06a4308_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - packaging=24.2=py39h06a4308_0
  - pip=25.0=py39h06a4308_0
  - pluggy=1.5.0=py39h06a4308_0
  - pytest=8.3.4=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py39h06a4308_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - certifi==2025.1.31
      - chardet==5.2.0
      - charset-normalizer==3.4.1
      - coverage==7.8.0
      - dataproperty==1.1.0
      - idna==3.10
      - mbstrdecoder==1.1.4
      - pathvalidate==3.2.3
      - pytablewriter==1.2.1
      - pytest-cov==6.0.0
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - requests==2.32.3
      - six==1.17.0
      - tabledata==1.3.4
      - tcolorpy==0.1.7
      - typepy==1.3.4
      - urllib3==2.3.0
prefix: /opt/conda/envs/pypistats

tests/test_pypistats.py::TestPypiStats::test__sort tests/test_pypistats.py::TestPypiStats::test__sort_recent
tests/test_pypistats.py::TestPypiStats::test__tabulate
tests/test_pypistats.py::TestPypiStats::test__filter_end_date tests/test_pypistats.py::TestPypiStats::test__filter_no_filters_no_change tests/test_pypistats.py::TestPypiStats::test__filter_start_and_end_date tests/test_pypistats.py::TestPypiStats::test__filter_start_date tests/test_pypistats.py::TestPypiStats::test__paramify_bool tests/test_pypistats.py::TestPypiStats::test__paramify_float tests/test_pypistats.py::TestPypiStats::test__paramify_int tests/test_pypistats.py::TestPypiStats::test__paramify_none tests/test_pypistats.py::TestPypiStats::test__paramify_string tests/test_pypistats.py::TestPypiStats::test__total tests/test_pypistats.py::TestPypiStats::test__total_recent
pypistats/__init__.py
pypistats/__init__.py