Reputation: 29
I'm using python 2.7. I have a list of apps that I want to sort. My problem is this: I have a specific word that I need to be on top of the list and then rest will come.
my list is like this:
Now I used this code to sort by alphabetical order
with open(filename) as
sortedFile:
sortedFile = sortedFile.readlines()
sortedFile.sort()
I have no idea how to make it sort first by using the word "Boostrap"
Upvotes: 1
Views: 277
Reputation: 86
Building off this answer to a different question, you can set a function for the key to be sorted on:
from __future__ import print_function
items = ['Appname1', 'Appname2', 'Appname2Bootstrap', 'Appname3', 'Appname3Bootstrap']
items_sorted = sorted(items, key=lambda x: (not x.endswith('Bootstrap'), x))
print('Got the sorted items: {}'.format(items_sorted))
This will put the two items with "Bootstrap" at the beginning of your sorted list.
Upvotes: 2