I am with no math background so struggling in a simple thing.
from dataclasses import dataclass
@dataclass
class Country:
cc:str
name:str
countries_info = [
('DE', 'Germany'),
('EP', 'Europe'),
('US', 'United States'),
('IN', 'India')
]
countries = [Country(cc, name)
for cc, name in countries_info]
# sort based on a particular order
order = ['US', 'IN']
sorted(countries, key=lambda x: order.index(x.cc) if x.cc in order else -1)
Out put:
[Country(cc='DE', name='Germany'),
Country(cc='EP', name='Europe'),
Country(cc='US', name='United States'),
Country(cc='IN', name='India')]
What I want is
[Country(cc='US', name='United States'),
Country(cc='IN', name='India'),
Country(cc='DE', name='Germany'),
Country(cc='EP', name='Europe')]
Means only sort the countries based on order and make them appear first. do not touch the order of rest
In case of conflict like the same cc, this is desirable.
[Country(cc='US', name='United States'),
Country(cc='US', name='United States of America')
Country(cc='IN', name='Bharat'),
Country(cc='IN', name='India'),
Country(cc='DE', name='Germany'),
Country(cc='EP', name='Europe')]
Means It doesn't matter which comes first in case of conflict.