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

  • Home
  • SEARCH
  • 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 7638499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:11:36+00:00 2026-05-31T08:11:36+00:00

I have a list of lists of three strings in Python, e.g., m =

  • 0

I have a list of lists of three strings in Python, e.g.,

m = [['pig', 'quartz', '1'], ['pork', 'nails', '1'], ...]

and I want to sort it by index 0, then by index 2 in reverse order, then by index 1. At each step in the sort I’d like to maintain the order imposed by the other columns. For instance,

pork       barn         4
pork       barn2        4
pork       nails        1
pig        quartz       1
quinoa     pail         1
quinoa     quatern      1
quail      quatern      1
radish     barn         1
radish     barn2        1
radish     inbox        3
radish     snow         1

would become:

pig        quartz       1
pork       barn         4
pork       barn2        4
pork       nails        1
quail      quatern      1
quinoa     pail         1
quinoa     quatern      1
radish     inbox        3    <-
radish     barn         1
radish     barn2        1
radish     snow         1

that is, sort by the first column, then within each group of 1st columns (pig, pork, quail, …), sort by the third column reversed, then within each group of 1st-column-3rd-column ((pig, 1), (pork, 4), (pork, 1), …), sort by the second column.

How can I do this nicely? Conceptually, if operator.itemgetter() could encode sort order along with index, I would want something like m.sort(key=operator.itemgetter(0, -2, 1)).

  • 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-31T08:11:37+00:00Added an answer on May 31, 2026 at 8:11 am

    Use a custom comparison function:

    def compare_triples(a, b):
        ret=cmp(a[0], b[0])
        if ret: return ret
        ret=cmp(b[2], a[2])
        if ret: return ret
        return cmp(a[1], b[1])
    
    for i in m: print i
    print "-" * 79
    m2=sorted(m, cmp=compare_triples)
    for i in m2: print i
    

    The compare function is not optimal and might be rewritten as:

    def compare_triples(a, b):
        return cmp((a[0], b[2], a[1]), (b[0], a[2], b[1]))
    

    As others have pointed out this will work too:

    def sort_key(a):
        return a[0], -int(a[2]), a[1]
    
    for i in sorted(m, key=sort_key): print i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have three lists. Depending on user interaction, these list may either be available
I have a Python list variable that contains strings. Is there a function that
I have a list of strings in Python - elements . I would like
I have a function that searches a string in a list of lists then
i have an IList<Animals> farmAnimals; this list has three types, Cows Sheep Chickens how
I have this unordered list <ul> <li>two</li> <li>three</li> </ul> Is there a way I
I have a list of regexes in python, and a string. Is there an
I have a list of strings and a list of filters (which are also
I have three lists: old, new and ignore. old and new are lists of

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.