-1

I am a list

[['12', '2017/6/10'],
['-2', '2017/5/10'],
['12', '2017/2/10']]

I need to sorted by first element numerically, second on time (or use a given function say second letter of the string)

user1269298
  • 717
  • 2
  • 8
  • 26
  • 2
    Possible duplicate of [Sort a list by multiple attributes?](https://stackoverflow.com/questions/4233476/sort-a-list-by-multiple-attributes) – Wiggy A. Aug 27 '17 at 01:32

4 Answers4

4

Is this what you want ?

from operator import itemgetter
LIST=[['12', '2017/6/10'],
['-2', '2017/5/10'],
['12', '2017/2/10']]
sorted(LIST, key=itemgetter(0,1))

Out[127]: [['-2', '2017/5/10'], ['12', '2017/2/10'], ['12', '2017/6/10']]

EDIT1 sorry overlook the character to numeric

sorted(LIST, key=lambda x: (int(x[0]), x[1]))
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I think that this not work with `LIST=[['10', '2017/6/10'], ['-2', '2017/5/10'], ['12', '2017/2/10']]` –  Aug 27 '17 at 01:48
  • @gsi-frank out put `[['-2', '2017/5/10'], ['10', '2017/6/10'], ['12', '2017/2/10']] ` – BENY Aug 27 '17 at 01:49
  • @Wen I just run your solution with `LIST=[['10', '2017/6/10'], ['-2', '2017/5/10'], ['12', '2017/2/10']]` and the result was `[['10', '2017/6/10'], ['-2', '2017/5/10'], ['12', '2017/2/10']]`. Test it pls. –  Aug 27 '17 at 01:57
  • 1
    Your welcome. I still think that the date could be a problem when you compare them as strings, just image two digits months. Take a look at my solution to see what I did. –  Aug 27 '17 at 02:00
  • 1
    @gsi-frank as far as right now , op's sample date would not be a problem , but I prefer your answer , that is more coding wisely ~ less chance to have a error in the future running – BENY Aug 27 '17 at 02:02
2
from datetime import datetime

l = [['12', '2017/6/10'],
['-2', '2017/5/10'],
['12', '2017/2/10']]

l.sort(key=lambda x: (int(x[0]), datetime.strptime(x[1], "%Y/%m/%d")))

print(l)

Pls, take a look at "%Y/%m/%d" to see if that match your date format. Is not clear if the month or the day come first given the examples your used.

Besides, int(x[0]) could be some modifications depending on how long is your numerical values.

  • you don't need `datetime.strptime`, string comparison is good enough in this case – sKwa Aug 27 '17 at 01:56
  • @sKwa I don't think so ;) Just think in the case that the month or day are two digits. –  Aug 27 '17 at 02:01
  • @user1269298 If you can , please accept gsi-frank's answer ~ this is the way straightforward to your expected output :) – BENY Aug 27 '17 at 02:01
  • 1
    @Wen I would give you all the points if I could for so kind comment ;) –  Aug 27 '17 at 02:02
  • @gsi-frank: in general, you are right, but in particular examples I do not agree – sKwa Aug 27 '17 at 02:04
  • @gsi-frank we are here ready to help ~ not fight for the point right ? ~ :) have a nice day ~ oops , nice weekends ~;-) – BENY Aug 27 '17 at 02:04
  • 2
    @Wen I wish people understand what you said, stop fighting each other (as I have seemed in these sites) and start working in collaboration to provide the best help possible. For me, a thank or a single it works is more than enough because I have been on the other side often. Thanks again for so kind comments. –  Aug 27 '17 at 02:08
  • @gsi-frank can not agree more ~ :) – BENY Aug 27 '17 at 02:10
0

Use this :

sorted(a, lambda x,y: x[0]<y[0] or x[0]==y[0] and x[1]<y[1]) 

if you want to do a special cmp function on the second key, you could define a custom cmp function , E.g.

def cmp(sa,sb):
     return sa[3] < sb[3]

sorted(a, lambda x,y: x[0]<y[0] or x[0]==y[0] and cmp(x[1],y[1]))
GuangshengZuo
  • 4,447
  • 21
  • 27
-1

I think the solution is using 'key' argument to define sorting key and 'cmp' argument to define function

user1269298
  • 717
  • 2
  • 8
  • 26