0

I have following problem:

List to be sorted:

[['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'], 
['[check41] Ensure ...', 'Found ...']]

Pattern:

["[check28] Ensure ...",
"[check116] Ensure ...",
"[check41] Ensure ..."]

Desired output:

[['[check28] Ensure ...', 'eu-west-1: Key ...', 'eu-central-1: Key ...'], 
['[check116] Ensure ...', 'azure-ad-role-manager ...'],
['[check41] Ensure ...', 'Found ...']]

I've tried: Sorting list based on values from another list and few other solutions - but they mainly base on sorting the pattern's int values, and it's not the case in my problem.

Thanks in advance for any hints or solutions.

hohenheim
  • 33
  • 4

1 Answers1

1

You can use list.index in your key= function:

lst = [
    ["[check116] Ensure ...", "azure-ad-role-manager ..."],
    ["[check28] Ensure ...", "eu-west-1: Key ...", "eu-central-1: Key ..."],
    ["[check41] Ensure ...", "Found ..."],
]

pattern = [
    "[check28] Ensure ...",
    "[check116] Ensure ...",
    "[check41] Ensure ...",
]

out = sorted(lst, key=lambda k: pattern.index(k[0]))
print(out)

Prints:

[
    ["[check28] Ensure ...", "eu-west-1: Key ...", "eu-central-1: Key ..."],
    ["[check116] Ensure ...", "azure-ad-role-manager ..."],
    ["[check41] Ensure ...", "Found ..."],
]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91