i have the following snip of data from i file i read in:

li=[[4, 27, 6, 22, 0.81186094, 1.12214581, 2],
[1, 9, 22, 28, 0.69887889, 0.989934, 2],
[4, 27, 100, 30, 0.74796748, 1.04485376, 2],
[6, 27, 22, 31, 0.91040724, 1.26681591, 2],
[4, 1, 30, 45, 0.485223, 0.77061901, 2],
[8, 27, 22, 46, 0.94473684, 1.33349425, 2],
[5, 27, 22, 50, 0.81594684, 1.1273952, 2],
[4, 27, 1, 51, 0.54467742, 0.83013525, 2],
[7, 108, 22, 53, 0.69060585, 0.98095148, 2],
[4, 28, 22, 4, 0.775569, 1.09176979, 2]]

i have used this function:
li.sort(lambda x, y: cmp(x[0],y[0]))

this sorts my file by the first so my output is

[[1, 9, 22, 28, 0.69887889000000003, 0.98993399999999998, 2], [4, 27, 6, 22, 0.81186093999999998, 1.1221458099999999, 2],
[4, 27, 100, 30, 0.74796748000000002, 1.0448537600000001, 2], [4, 1, 30, 45, 0.48522300000000002, 0.77061900999999999, 2], [4, 27, 1, 51, 0.54467741999999997, 0.83013524999999999, 2], [4, 28, 22, 4, 0.77556899999999995, 1.0917697900000001, 2],
[5, 27, 22, 50, 0.81594683999999995, 1.1273952, 2],
[6, 27, 22, 31, 0.91040723999999995, 1.26681591, 2],
[7, 108, 22, 53, 0.69060584999999997, 0.98095147999999999,2],
[8, 27, 22, 46, 0.94473684000000002, 1.33349425, 2]]

so this sorts by the 1st number. i would like to sort by the first 4 values

so my out put would be as follows
1 9 22 28 0.69887889 0.989934 2
4 1 30 45 0.485223 0.77061901 2
4 27 1 51 0.54467742 0.83013525 2
4 27 6 22 0.81186094 1.12214581 2
4 27 100 30 0.74796748 1.04485376 2
4 28 22 4 0.7875569 1.09176979 2
5 27 22 50 0.81594684 1.1273952 2
6 27 22 31 0.91040724 1.26681591 2
7 108 22 53 0.69060585 0.98095148 2
8 27 22 46 0.94473684 1.33349425 2


please help???

If you tried li.sort(lambda x, y: cmp(x[0],y[0])) , then you must have thought to try li.sort(lambda x, y: cmp(x,y)) , which happens to work.

oh dear thank you so much i had assumed i had to put in values thank you thank you

You can use this:

mylist = [
[1, 9, 22, 28, 0.69887889000000003, 0.98993399999999998, 2],
[4, 27, 6, 22, 0.81186093999999998, 1.1221458099999999, 2],
[4, 27, 100, 30, 0.74796748000000002, 1.0448537600000001, 2],
[4, 1, 30, 45, 0.48522300000000002, 0.77061900999999999, 2],
[4, 27, 1, 51, 0.54467741999999997, 0.83013524999999999, 2],
[4, 28, 22, 4, 0.77556899999999995, 1.0917697900000001, 2],
[5, 27, 22, 50, 0.81594683999999995, 1.1273952, 2],
[6, 27, 22, 31, 0.91040723999999995, 1.26681591, 2],
[7, 108, 22, 53, 0.69060584999999997, 0.98095147999999999,2],
[8, 27, 22, 46, 0.94473684000000002, 1.33349425, 2]]

# inplace sort by the 4th item which is at index 3
ix = 3
mylist.sort(key=lambda x: x[ix])

for item in mylist:
    print(item)

""" result =
[4, 28, 22, 4, 0.775569, 1.09176979, 2]
[4, 27, 6, 22, 0.81186094, 1.12214581, 2]
[1, 9, 22, 28, 0.69887889, 0.989934, 2]
[4, 27, 100, 30, 0.74796748, 1.04485376, 2]
[6, 27, 22, 31, 0.91040724, 1.26681591, 2]
[4, 1, 30, 45, 0.485223, 0.77061901, 2]
[8, 27, 22, 46, 0.94473684, 1.33349425, 2]
[5, 27, 22, 50, 0.81594684, 1.1273952, 2]
[4, 27, 1, 51, 0.54467742, 0.83013525, 2]
[7, 108, 22, 53, 0.69060585, 0.98095148, 2]
"""

thanks ok i have another problem if i am reading in a file how can i do this my function seems to be sorting within the list not mylist as a whole???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.