-1

I am having trouble finding any questions and answers to this.

I have a list of words as below:

['ABC11-2', 'BCD14-1', 'ABC09-1', 'ABC14-1', 'BCD11-2']

I need to sort it like so

['ABC09-1', 'ABC14-1', ABC11-2', 'BCD14-1', 'BCD11-2']

But with sorted() or sort() I get

['ABC09-1','ABC11-2', ABC14-1', 'BCD11-2', 'BCD14-1']

I need it to sort by the unit name first, then the semester and then the unit number.

Thanks,

Willdomybest18
  • 127
  • 3
  • 12
  • have a look at this: https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside – Adnan Hadi Aug 05 '20 at 03:19
  • Are the unit names always 3 characters and the semester always 1 digit at the end? – Craig Aug 05 '20 at 03:44

2 Answers2

4

You can use key argument to sorted, which will specify the order of sorting:

lst = ['ABC11-2', 'BCD14-1', 'ABC09-1', 'ABC14-1', 'BCD11-2']

def f(x):
    x = x.split('-')
    return x[0][:3], int(x[1]), int(x[0][3:])
    
print(sorted(lst, key=f))
# ['ABC09-1', 'ABC14-1', 'ABC11-2', 'BCD14-1', 'BCD11-2']
Austin
  • 25,759
  • 4
  • 25
  • 48
  • Unfortunately this did not work. It still puts the 2 before the 1 – Willdomybest18 Aug 05 '20 at 03:16
  • I have also added the output of my code as the last line. Could you please check again? – Austin Aug 05 '20 at 03:17
  • It works, but I have one issue. What is the number before the hyphen matters? for example if there was an extra element ```ABC09-1``` how will this be sorted? – Willdomybest18 Aug 05 '20 at 03:20
  • It has to be done differently. Sorry, but your original question didn't cover that. How would you want that to be sorted? – Austin Aug 05 '20 at 03:24
  • Your question was *"It sorts by the string but I need it to sort by the number after the hyphen"*. – Austin Aug 05 '20 at 03:25
  • It didnt, my bad just thought about it then. Its basically unit codes for college. The hyphen number represents the semester. So I would assume that it should look like this ```[''ABC09-1' , ABC14-1', 'ABC11-2']``` So the sort is first by the letters, then by the semester and then by the unit code number – Willdomybest18 Aug 05 '20 at 03:27
  • Please update your question with full details, sample input and output. Hence, it won't be confusing to future readers. As of now if I answer, to your comment, it would be wrong as per the question. – Austin Aug 05 '20 at 03:33
  • Updated the question to be specific – Willdomybest18 Aug 05 '20 at 03:42
  • Thanks for the update. Please find your answer (edited just now). – Austin Aug 05 '20 at 03:43
  • It works! But... for some reason with some other units the unit semester is begin sorted primarily. I assume the function ```f()``` first sorts alphabetically , then by semester and then my unit number? – Willdomybest18 Aug 05 '20 at 03:52
  • Yes, it sorts as you want it to be. The `return` line in the function specifies that we want it to be ordered by alphabets, number after hyphen and then by number before hyphen. – Austin Aug 05 '20 at 03:59
0

From the question, it looks like the unit name is 3 characters at the beginning and the semester is 1 character at the end. Assuming this is the case, then you can re-arrange the characters in the course names and pass that to the key parameter of sorted(). This is simple enough that you can use a lambda function for the transform.

lst = ['ABC11-2', 'BCD14-1', 'ABC09-1', 'ABC14-1', 'BCD11-2']

print(sorted(lst, key=lambda s: s[:3] + s[-1] + s[3:-1]))

Outputs:

['ABC09-1', 'ABC14-1', 'ABC11-2', 'BCD14-1', 'BCD11-2']
Craig
  • 4,605
  • 1
  • 18
  • 28