Search code examples
pythonsortinggoogle-scholar

Sorting a list of publications by year


I'm am growing a list of publications authored by a specific person featured on Google Scholar, using scholarly PyPi module (pub in author.publications).

from scholarly import scholarly

search_query = scholarly.search_author('John Doe')
author = next(search_query).fill()
c = 0
list = []

Each line of the list comprises three items : 1- an iteration number using a counter,(c) 2- Title, 3- Year. The list can then be displayed by order of relevance (iteration number).

Once completed, I want to sort the list by release year. Here are two unsuccessful attempts as the list comes out in the same order it previously did (by relevance). If you're able to see what I did wrong... Thanks!

ATTEMPT no.1

for pub in author.publications:
    try:
        c += 1                                                    # c = c + 1
        print(c, pub.bib['title'], '(', pub.bib['year'], ')')     # displayed in relevance order
        list.append([pub.bib['title'],pub.bib['year']])           # modifies/expands the list
        def get_year(list):
            return list.get(pub.bib['year'])
        list.sort(key=get_year)                                   # re-arrange the list by release year

    except:
        pass


for l in list:                                                    # should print each line of the new list, but instead displays the original
    print(l)

ATTEMPT no.2

for pub in author.publications:
    try:
        c += 1                                                     # c = c + 1
        print(c, pub.bib['title'], '(', pub.bib['year'], ')')      # displayed in relevance order 
        list.append([pub.bib['title'],pub.bib['year']])            # modifies/expands the list
        list.sort(key=[pub.bib['year']])                           # re-arrange the list by release year                                  

    except:
        pass

for l in list:
    print(l)                                                       # should print each line of the new list, but instead displays the original

Solution

  • df.sort() method? have you tried this