Supposing I have a list/tuple like this:
MyLocation = 'DE'
(
('Pencils', 'Artists Pencils', 18.95, 'PVT', 'DE'),
('Pencils', '', 19.95, 'PVT', 'IT'),
('Pencils', '', 23.50, 'PRF1', 'US'),
('Pencils', 'Wooden Pencils', 23.50, 'PRF2', 'DE'),
('Pencils', '', 12.50, 'NON', 'DE'))
I want to sort this in two passes, by the following rules:
1) Tuples matching the MyLocation string 'DE'
in the [4]
element, on top
This being an intermediate step, the relative order between the DE
s doesn't matter. Just so that all DE
s are at the top.
(
('Pencils', '', 12.50, 'NON', 'DE'),
('Pencils', 'Wooden Pencils', 23.50, 'PRF2', 'DE'),
('Pencils', 'Artists Pencils', 18.95, 'PVT', 'DE'),
('Pencils', '', 23.50, 'PRF1', 'US'),
('Pencils', '', 19.95, 'PVT', 'IT')
)
2) After that, sort on the [3]
rd element, the preferred order should be ['PRF1', 'PRF2', 'PRF3']
. Other strings can be left at lower positions.
My expected final sorted output is
(
('Pencils', '', 23.50, 'PRF1', 'US'),
('Pencils', 'Wooden Pencils', 23.50, 'PRF2', 'DE'),
('Pencils', 'Artists Pencils', 18.95, 'PVT', 'DE'),
('Pencils', '', 12.50, 'NON', 'DE'),
('Pencils', '', 19.95, 'PVT', 'IT')
)
How would I go about these two sorts? I can manage the first sorting with del and insert, but what is the recommended way?
tempList = actualList
i = 0
for record in actualList:
if record[5] == 'DE':
del tempList[i]
tempList.insert(0, record)
i = i + 1
actualList = tempList
I am especially confused about how I would proceed with the second sorting. Please provide code samples for the second sorting.