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 8446881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:02:28+00:00 2026-06-10T10:02:28+00:00

I have a multi-column table of data, each row unique, and I want to

  • 0

I have a multi-column table of data, each row unique, and I want to know how to sort it based on multiple columns. If sorting alphabetically a solution is already described here. However, instead of alphabetical sorting, I need to sort each column based on a custom ordering stored in another list. For example, if my table is

mytable = [
    ('A1', 'B1', 'C1'),
    ('A1', 'B2', 'C2'),
    ('A2', 'B2', 'C1'),
    ('A2', 'B2', 'C2')
]

I might want the first column to be ordered [‘A2′,’A1’], the second column to be ordered [‘B1′,’B2’], and the third column to be ordered [‘C2′,’C1’]. The proper result would be

mytable = [
    ('A2', 'B2', 'C2'),
    ('A2', 'B2', 'C1'),
    ('A1', 'B1', 'C1'),
    ('A1', 'B2', 'C2')
]
  • 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-06-10T10:02:30+00:00Added an answer on June 10, 2026 at 10:02 am

    This will do what you’re looking for:

    orderings = (
        ('A2', 'A1'),
        ('B1', 'B2'),
        ('C2', 'C1')
    )
    
    orders = [dict([(v, i) for i, v in enumerate(o)]) for o in orderings]
    
    mytable.sort(key=lambda r: tuple(o[c] for c, o in zip(r, orders)))
    

    In practice, the columns might not all be sorted, or might be sorted in a priority other than left-right order. That could be solved by attaching an index to each and adapting the algorithm accordingly.

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

Sidebar

Related Questions

I have a large (multi-GB) data file exported from an Oracle table. I want
I have a custom, multi-column sort attached to my jqGrid instance by means of
I have a multi-table SELECT query which compares column values with itself like below:
I have a situation where I want to associate multiple values from one table
In a multi-tenant application I have a table with TenantId column. This table is
In my table, I have a varchar column whereby multi-values are stored. An example
Let's say I have a data.table and I want to select all the rows
I have created a multi column datastore on a table that allows me to
Question: Is it possible to have enforce multiple column constraint when creating a table
I'm using 'rails3-jquery-autocomplete' gem, but it doesn't have multi column search, but there is

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.