You can call operator.itemgetter() as a sort_key value. Note that sortby still needs to be given for the sort_key to be applied:

import operator
from prettytable import PrettyTable


table = PrettyTable(["Name", "Grade"])
table.add_row(["Joe", 90])
table.add_row(["Sally", 100])
table.add_row(["Bill", 90])
table.add_row(["Alice", 90])
print table.get_string(sort_key=operator.itemgetter(1, 0), sortby="Grade")

Prints:

+-------+-------+
|  Name | Grade |
+-------+-------+
| Alice |   90  |
|  Bill |   90  |
|  Joe  |   90  |
| Sally |  100  |
+-------+-------+
Answer from alecxe on Stack Overflow
🌐
ZetCode
zetcode.com › python › prettytable
Python PrettyTable - generating tables in Python with PrettyTable
January 29, 2024 - The reversesort property controls the direction of sorting (ascending vs descending). ... #!/usr/bin/python from prettytable import PrettyTable x = PrettyTable() x.field_names = ["City name", "Area", "Population", "Annual Rainfall"] x.add_row(["Adelaide", 1295, 1158259, 600.5]) x.add_row(["Brisbane", 5905, 1857594, 1146.4]) x.add_row(["Darwin", 112, 120900, 1714.7]) x.add_row(["Hobart", 1357, 205556, 619.5]) x.add_row(["Sydney", 2058, 4336374, 1214.8]) x.add_row(["Melbourne", 1566, 3806092, 646.9]) x.add_row(["Perth", 5386, 1554769, 869.4]) print("Table sorted by population:") x.sortby = "Population" print(x) print() print("Table sorted by city in descendig order:") x.sortby = "City name" x.reversesort = True print(x)
🌐
Readthedocs
ptable.readthedocs.io › en › latest › tutorial.html
Tutorial — PTable 0.9.0 documentation
The first element will be whatever data is in the relevant row, in the column specified by the sort_by argument. The remaining n elements are the data in each of the table’s columns, in order, including a repeated instance of the data in the sort_by column. By default, PrettyTable produces ...
🌐
HotExamples
python.hotexamples.com › examples › prettytable › PrettyTable › sortby › python-prettytable-sortby-method-examples.html
Python PrettyTable.sortby Examples, prettytable.PrettyTable.sortby Python Examples - HotExamples
Current Results....') pt = PrettyTable(['ACCOUNT', 'USER', 'RESULT', 'INSTANCES']) pt.align = 'l' pt.max_width['INSTANCES'] = 11 + max_zone_name + 1 pt.sortby = 'ACCOUNT' for user in users: if user.results: res = user.results pt.add_row([user.account_name, user.user_name, res.get('result'), res.get('instances')]) print "\n{0}\n".format(pt) tt = PrettyTable(['CLC VETH', 'CREATE TIME']) tt.align = 'l' tt.hrules = 1 for key, value in clc_times.iteritems(): tt.add_row([key, value]) print "\n{0}\n".format(tt) mdt = PrettyTable(['INSTANCE', 'MIDO MD RULE TIME']) mdt.align = 'l' mdt.hrules = 1 for key, value in mido_md_times.iteritems(): mdt.add_row([key, value]) print "\n{0}\n".format(mdt)
🌐
Google Code
code.google.com › archive › p › prettytable › wikis › Tutorial.wiki
Google Code Archive - Long-term storage for Google Code Project Hosting.
Archive · Skip to content · The Google Code Archive requires JavaScript to be enabled in your browser · Google · About Google · Privacy · Terms
🌐
PyPI
pypi.org › project › prettytable
prettytable · PyPI
The remaining n elements are the data in each of the table's columns, in order, including a repeated instance of the data in the sort_by column. You can divide your table into different sections using the add_divider method or divider argument to add_row() or even to add_rows(). This will add a dividing line into the table under the row who has this field set. So we can set up a table like this: table = PrettyTable() table.field_names = ["City name", "Area", "Population", "Annual Rainfall"] table.add_row(["Adelaide", 1295, 1158259, 600.5]) table.add_divider() table.add_row(["Brisbane", 5905, 1857594, 1146.4], divider=True) table.add_rows( [["Darwin", 112, 120900, 1714.7], ["Hobart", 1357, 205556, 619.5]], divider=True ) table.add_row(["Melbourne", 1566, 3806092, 646.9]) table.add_row(["Perth", 5386, 1554769, 869.4]) table.add_row(["Sydney", 2058, 4336374, 1214.8])
      » pip install prettytable
    
Published   Nov 14, 2025
Version   3.17.0
🌐
CSDN
devpress.csdn.net › python › 63046336c67703293080c361.html
Python prettytable Sort by Multiple Columns - DevPress官方社区
August 23, 2022 - import operator from prettytable import PrettyTable table = PrettyTable(["Name", "Grade"]) table.add_row(["Joe", 90]) table.add_row(["Sally", 100]) table.add_row(["Bill", 90]) table.add_row(["Alice", 90]) print table.get_string(sort_key=operator.itemgetter(1, 0), sortby="Grade") Prints: +-------+-------+ | Name | Grade | +-------+-------+ | Alice | 90 | | Bill | 90 | | Joe | 90 | | Sally | 100 | +-------+-------+ # python ·
Find elsewhere
🌐
GitHub
github.com › vishvananda › prettytable › blob › master › prettytable.py
prettytable/prettytable.py at master · vishvananda/prettytable
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"]) x.sortby = "Population" x.reversesort = True · x.int_format["Area"] = "04" x.float_format = "6.1" x.align["City name"] = "l" # Left align city names ·
Author   vishvananda
🌐
HotExamples
python.hotexamples.com › examples › prettytable › PrettyTable › sort_key › python-prettytable-sort_key-method-examples.html
Python PrettyTable.sort_key Examples, prettytable.PrettyTable.sort_key Python Examples - HotExamples
import sh TIME_ZONE = 'Asia/Shanghai' date = Delorean(timezone=TIME_ZONE) print(date.datetime) # prettytable 浏览器或终端构造表格 from prettytable import PrettyTable from colorama import Fore table = PrettyTable(field_names=["name", "age", "lang", "schollnae", "nowTime"]) table.align["name"] = "1" table.padding_width = 1 redName = Fore.RED + "mayl" + Fore.RESET table.add_row([redName, 26, "python", "YBU", date.datetime]) table.add_row(["mayt", 23, "python", "ZJU", date.datetime]) table.sort_key("age") # table.reversesort = True print(table) # python中使用shell命令,用os.popen()执行命令cmd,还有其他方法。。。。。 import os output = os.popen("ls $HOME/bin") print(output.read(), type(output)) # ----------- from sh import ifconfig print(ifconfig("wlan0")) # progressbar是一个进度条库,该库提供了一个文本模式的progressbar。
🌐
LearnModernPython
learnmodernpython.com › home › mastering the ‘prettytable’ library: python’s data formatting powerhouse
Mastering The 'PrettyTable' Library: Python's Data Formatting Powerhouse
February 25, 2026 - PrettyTable’s built-in sorting is limited to a single column. If you need to sort by multiple columns, you’ll need to sort the data before adding it to the table. You can use Python’s built-in sorted() function with a custom key to sort ...
🌐
GitHub
github.com › vishvananda › prettytable › blob › master › README
prettytable/README at master · vishvananda/prettytable
PrettyTable will also print your tables in HTML form, as `<table>`s. Just like · in ASCII form, you can actually print your table - just use `print_html()` - or · get a string representation - just use `get_html_string()`. HTML printing · supports the `fields`, `start`, `end`, `sortby` and `reversesort` arguments in
Author   vishvananda
🌐
Readthedocs
gemseo.readthedocs.io › en › 5.1.0 › _modules › gemseo.third_party.prettytable.prettytable.html
prettytable module — GEMSEO 5.1.0 documentation
PrettyTable.sort_key · PrettyTable.sortby · PrettyTable.start · PrettyTable.title · PrettyTable.valign · PrettyTable.vertical_char · PrettyTable.vrules · gemseo / third_party / prettytable · Hide inherited members · class gemseo.third_party.prettytable.prettytable.PrettyTable(field_names=None, **kwargs)[source]¶ ·
🌐
GitHub
github.com › kxxoling › PTable › blob › master › prettytable › prettytable.py
PTable/prettytable/prettytable.py at master · kxxoling/PTable
self._options = "title start end fields header border sortby reversesort sort_key attributes format hrules vrules".split()
Author   kxxoling
🌐
Cloudera
cloudera.github.io › cm_api › epydoc › 4.7.0 › cm_shell.prettytable.PrettyTable-class.html
cm_shell.prettytable.PrettyTable
Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__ · Inherited from object: __class__
🌐
Xbuba
xbuba.com › questions › 37423445
xbuba.com - xbuba المصادر والمعلومات.
May 24, 2016 - xbuba.com هل هو أول وأفضل مصادر جميع المعلومات التي تبحث عنها. من ضوء الموضوعات العامة إلى مزيد من الموضوعات التي كنت لتتوقعها هنا، xbuba.com تمتلكها جميعًا.
🌐
Snyk
snyk.io › advisor › prettytable › functions › prettytable.prettytable
How to use the prettytable.PrettyTable function in prettytable | Snyk
def listmultiparts(args): vault_name = args.vault region = args.region glacierconn = glaciercorecalls.GlacierConnection(args.aws_access_key, args.aws_secret_key, region=region) if check_vault_name(vault_name): response = glaciercorecalls.GlacierVault(glacierconn, vault_name).list_multipart_uploads() parse_response(response) jdata = json.loads(response.read()) print "Marker: ", jdata['Marker'] if len(jdata['UploadsList']) > 0: headers = sorted(jdata['UploadsList'][0].keys()) table = PrettyTable(headers) for entry in jdata['UploadsList']: table.add_row([locale.format('%d', entry[k], grouping=True) if k == 'PartSizeInBytes' else entry[k] for k in headers ]) print table ·
🌐
GitHub
github.com › nickl- › prettytable › wiki › Tutorial
Tutorial · nickl-/prettytable Wiki · GitHub
October 11, 2012 - and the sorting will happen each time. You can turn sorting off later with ... Similarly, you can set reversesort to be permanently True or False in the same way. PrettyTable supports lots of formatting options to control the appearance of your table.
Author   nickl-
🌐
Itecnote
itecnote.com › tecnote › python-prettytable-sort-by-multiple-columns
Python prettytable Sort by Multiple Columns
Skip to content · React-native – React Native Error: “Animated.event now requires a second argument for options” · Python – keep on getting “x and y must have same first dimension, but have shapes (100,) and (1, 100)” · Java – Docker WARNING: Published ports are discarded when ...