## sab786

i have a snip of my file below can the the numbers be sorted as in excel?

27 4 220 16 0.76151102 1.06059456
27 4 220 17 0.61465055 0.90107796
27 4 221 22 0.81186094 1.12214581
27 4 223 28 0.69887889 0.98993400
27 4 225 30 0.74796748 1.04485376

## scru 909

Yes, they can.

commented: haha! :) +4

## bumsfeld 413

You have to convert your data string to list of lists and then sort the sublists by item index:

``````s = """\
27 4 220 16 0.76151102 1.06059456
27 4 220 17 0.61465055 0.90107796
27 4 221 22 0.81186094 1.12214581
27 4 223 28 0.69887889 0.98993400
27 4 225 30 0.74796748 1.04485376"""

# create list of lists from the data string
mylist = []
for line in s.splitlines():
temp_list = []
for item in line.split():
temp_list.append(eval(item))
mylist.append(temp_list)

print("Original lists:")
for sublist in mylist:
print(sublist)

print('-'*40)

print("Sorted by the 5th item:")
# inplace sort by the 5th item which is at index 4
ix = 4
mylist.sort(key=lambda x: x[ix])
for sublist in mylist:
print(sublist)

"""result=
Original lists:
[27, 4, 220, 16, 0.76151102, 1.06059456]
[27, 4, 220, 17, 0.61465055, 0.90107796]
[27, 4, 221, 22, 0.81186094, 1.12214581]
[27, 4, 223, 28, 0.69887889, 0.989934]
[27, 4, 225, 30, 0.74796748, 1.04485376]
----------------------------------------
Sorted by the 5th item:
[27, 4, 220, 17, 0.61465055, 0.90107796]
[27, 4, 223, 28, 0.69887889, 0.989934]
[27, 4, 225, 30, 0.74796748, 1.04485376]
[27, 4, 220, 16, 0.76151102, 1.06059456]
[27, 4, 221, 22, 0.81186094, 1.12214581]
"""``````