{"id":2431,"date":"2026-11-15T07:34:56","date_gmt":"2026-11-14T23:34:56","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2431"},"modified":"2026-03-29T10:41:37","modified_gmt":"2026-03-29T10:41:37","slug":"how-to-use-rangesort-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangesort-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Sort in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Sort<\/code> method of the <code>Range<\/code> object in Excel, accessible through the xlwings API, is a powerful tool for organizing data within a specified range. Its primary function is to sort a range of cells based on one or more keys (columns or rows), allowing for both ascending and descending order, custom sort orders, and header recognition. This is essential for data analysis tasks where data needs to be ordered by specific criteria, such as sorting sales figures by date or alphabetizing a list of names.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, the syntax for the <code>Sort<\/code> method mirrors the VBA object model but follows Python conventions. The general call format is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range.sort(key1, order1, key2, order2, key3, order3, header, ordercustom, matchcase, orientation, sortmethod, dataoption1, dataoption2, dataoption3)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parameters are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>key1<\/code>, <code>key2<\/code>, <code>key3<\/code>: These are <code>Range<\/code> objects specifying the first, second, and third sort fields (the columns or rows to sort by). Only <code>key1<\/code> is required.<\/li>\n\n\n\n<li><code>order1<\/code>, <code>order2<\/code>, <code>order3<\/code>: Constants that determine the sort order for each key. Use <code>xlwings.constants.xlAscending<\/code> (or <code>'asc'<\/code> as a string) for ascending order and <code>xlwings.constants.xlDescending<\/code> (or <code>'desc'<\/code>) for descending.<\/li>\n\n\n\n<li><code>header<\/code>: Specifies whether the first row (or column) contains headers. Use <code>xlwings.constants.xlYes<\/code>, <code>xlNo<\/code>, or <code>xlGuess<\/code>.<\/li>\n\n\n\n<li><code>orientation<\/code>: Defines whether to sort by rows (<code>xlSortRows<\/code>, default) or columns (<code>xlSortColumns<\/code>).<\/li>\n\n\n\n<li><code>matchcase<\/code>: Set to <code>True<\/code> for case-sensitive sorting.<\/li>\n\n\n\n<li><code>sortmethod<\/code>: Can be <code>xlPinYin<\/code> (default for Chinese) or <code>xlStroke<\/code> for East Asian languages.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For most common sorts, you only need to specify the <code>key1<\/code> and <code>order1<\/code> parameters. The method sorts the entire original <code>range<\/code> object in place.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are two practical xlwings code examples:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 1: Basic Single-Column Sort<\/strong><br>This code sorts a data table in <code>Sheet1<\/code> based on the values in column B (the &#8220;Sales&#8221; column) in descending order, assuming the first row is a header.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('report.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Define the data range (A1 to C10, including headers)\ndata_range = sheet.range('A1:C10')\n\n# Sort by column B (index 2) in descending order\ndata_range.sort(key1=data_range.columns&#91;1], # B column is index 1 (0-based)\norder1=xw.constants.xlDescending,\nheader=xw.constants.xlYes)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 2: Multi-Column Sort<\/strong><br>This example performs a more complex sort on a list of employees. It first sorts by &#8220;Department&#8221; (column A) in ascending order, and then within each department, sorts by &#8220;Name&#8221; (column B) in ascending order.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('employees.xlsx')\nsheet = wb.sheets&#91;'Data']\nmain_range = sheet.range('A1:C100')\n\n# Multi-key sort: Department first, then Name\nmain_range.sort(key1=main_range.columns&#91;0], # Column A\norder1='asc', # String shortcut for ascending\nkey2=main_range.columns&#91;1], # Column B\norder2=xw.constants.xlAscending,\nheader=xw.constants.xlYes)\nwb.save()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Sort` method of the `Range` object in Excel, accessible through the xlwings API, is a powerful &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2431","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2431"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2431\/revisions"}],"predecessor-version":[{"id":3692,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2431\/revisions\/3692"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}