1

i'm uses this

ThreadList.append([''.join(map(str,[Thread])), date, rrf[5]])

SendThreadList = ''

ThreadList = sorted(ThreadList, key=lambda Entry: Entry[1], reverse=True)
ThreadList = sorted(ThreadList, key=lambda Entry: Entry[2], reverse=True)

sorted show only the second sorted, how to do sorted with two entrys in key ??

2
  • sorry for my bad english, i'm brazilian Commented Jan 6, 2013 at 21:03
  • What are you trying to achieve with ''.join(map(str,[Thread]))? Applying map, I get ''.join([str(Thread)]) which of course is just str(Thread) Commented Jan 6, 2013 at 21:21

2 Answers 2

6

If you want to sort by Entry[1], then Entry[2] then you make the lambda return both as a tuple...:

ThreadList = sorted(ThreadList, key=lambda Entry: (Entry[1], Entry[2]), reverse=True)

What's sometimes more readable (and potentially slightly quicker) is to make use of the operator module and use key=operator.itemgetter(1, 2)

Sign up to request clarification or add additional context in comments.

4 Comments

@GeorgeSet. Sorry - I don't understand - could you try re-phrasing please?
ThreadList = sorted(ThreadList, key=lambda Entry: (Entry[1], Entry[2]), reverse=True)
@GeorgeSet. What I don't get is "check the Entry[2] is 1" ?
@GeorgeSet. Yes, but it may be better as a new question with some context about what you're trying to achieve.
1

While it is the wrong way to do it, your example will work if you swap the order:

ThreadList = sorted(ThreadList, key=lambda Entry: Entry[2], reverse=True)
ThreadList = sorted(ThreadList, key=lambda Entry: Entry[1], reverse=True)

Python sorts are stable - if two entries tie on [1], they will keep their order from the first sort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.