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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:23:35+00:00 2026-06-11T14:23:35+00:00

I need to sort the catalog results by multiple fields. In my case, first

  • 0

I need to sort the catalog results by multiple fields.

In my case, first sort by year, then by month. The year and month field are included in my custom content type (item_publication_year and item_publication_month respectively).

However, I’m not getting the results that I want. The year and month are not ordered at all. They should appear in descending order i.e. 2006, 2005, 2004 etc.

Below is my code:

def queryItemRepository(self):
    """
    Perform a search returning items matching the criteria
    """

    query = {}

    portal_catalog = getToolByName(self, 'portal_catalog')
    folder_path = '/'.join( self.context.getPhysicalPath() )

    query['portal_type'] = "MyContentType"
    query['path'] = {'query' : folder_path, 'depth' : 2 }

    results = portal_catalog.searchResults(query)

    # convert the results to a python list so we can use the sort function
    results = list(results)  

    results.sort(lambda x, y : cmp((y['item_publication_year'], y['item_publication_year']), 
                                   (x['item_publication_month'], x['item_publication_month'])
                                  ))


    return results

Anyone care to help?

  • 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-11T14:23:36+00:00Added an answer on June 11, 2026 at 2:23 pm

    A better bet is to use the key parameter for sorting:

    results.sort(key=lambda b: (b.item_publication_year, b.item_publication_month))
    

    You can also use the sorted() built-in function instead of using list(); it’ll return a sorted list for you, it’s the same amount of work for Python to first call list on the results, then sort, as it is to just call sorted:

    results = portal_catalog.searchResults(query)
    results = sorted(results, key=lambda b: (b.item_publication_year, b.item_publication_month))
    

    Naturally, both item_publication_year and item_publication_month need to be present in the catalog metadata.

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

Sidebar

Related Questions

I need to sort a flat file by third column leaving first column intact
I need to sort on a date-field type, which name is mod_date. It works
I need to sort then print the result increasing and decreasing. I have some
I need to sort the results I get back from apache solr based on
I need to sort many files and then dump into many files in order
I need to sort a field name of that contains some name, NULL and
I need to sort my view by a specific field that is actually a
I need to sort an array in PHP and then sort by its second
I need to sort my custom class based on the std struct tm object.
I need to sort list of strings in the alphabetical order: List<String> list =

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.