Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 704547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:57:14+00:00 2026-05-14T03:57:14+00:00

I have a necessity to sort a given HTML table of the following structure,

  • 0

I have a necessity to sort a given HTML table of the following structure, in Python.

<table>
    <tr>
        <td><a href="#">ABCD</a></td>
        <td>A23BND</td>
        <td><a title="ABCD">345345</td>
    </tr>
    <tr>
        <td><a href="#">EFG</a></td>
        <td>Add4D</td>
        <td><a title="EFG">3432</td>
    </tr>
    <tr>
        <td><a href="#">HG</a></td>
        <td>GJJ778</td>
        <td><a title="HG">2341333</td>
    </tr>

</table>

I am doing something like this:

container = tree.findall("tr")
strOut = ""
data = []
for elem in container:
    key = elem.findtext(colName)
    data.append((key, elem))

data.sort()

The problem is that it sorts by the text inside the <td>. I want to be able to sort by the anchor value and not href.

What can I do to achieve that? Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:57:15+00:00Added an answer on May 14, 2026 at 3:57 am

    It sorts by the text because that’s what you’re extracting as the key when you do

    key = elem.findtext(colName)
    

    I imagine colName is some tag string, and findtext will just find the text of the first subelement matching that tag. If what you want instead is to use as the key the value of some attribute (e.g. title?) of an <a>,

    for ana in elem.findall('a'):
        key = ana.get('title')
        if key is not None: break
    

    Would do that. Exactly what do you want to use as the key?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table schema similar to the following (simplified): CREATE TABLE Transactions (
I have a key => value table I'd like to sort in Lua. The
I have to constantly sort a table by a field that has a decimal
I have been learning more and more javascript; it's a necessity at my job.
I have the following XML Document (that can be redesigned if necessary) that stores
I have 3 table: [Order, Order_to_goods, Goods] I want to get of all participants
I have 3 entities and each has a corresponding table. I have a task
We have a few existing web services which require some sort of basic security
I would like to number each row and NOT have those numbers move/sort. The
I am looking to implement a sort of 'activity log' table where actions a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.