0

I am using the Python sorted function in order to sort a multidimensional list which has many entries.

Example:

sorted_list = sorted(list_not_sorted, key=lambda x:x[1], reverse=True)

Is there a way to sort it based on the size of the numbers?

Lets say I have the following list:

[
[John,973],
[Jim,99],
[Jason,912345]
]

Using that code will sort it like this:

[
[Jim,99],
[John,973]
[Jason,912345],
]

However I want it sorted like this:

[
[Jason,912345],
[John,973]
[Jim,99],
]

Is there any way to do this with this function?

Question has been edited for clarity!

Web Hopeful
  • 741
  • 2
  • 6
  • 11
  • 1
    Did you mean for John and Jason to switch numbers, or is that an error? – user2357112 Nov 25 '13 at 01:55
  • Yes that was an error and I have edited it. – Web Hopeful Nov 25 '13 at 01:59
  • 1
    I dont see the problem here. For your input data, your code produces `[['Jason', 912345], ['John', 973], ['Jim', 99]]` – thefourtheye Nov 25 '13 at 02:00
  • FYI: You should be using tuples for your inner data structures, not lists. i.e. `[('John', 973), ('Jim', 99), ('Jason', 912345)]` See [this](http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples-in-python) for why. – Jonathon Reinhart Nov 25 '13 at 02:05
  • I don't think I can use anything else besides a list as I've extended and existing list to create this list I need to sort. – Web Hopeful Nov 25 '13 at 02:07
  • When you show example data, you should make it *very* clear whether it is actual Python code representation of data or not. Your half-way example (which isn't valid Python code) makes it appear the the second elements are integers, when they're probably strings. You should instead show an example of the actual *raw* data you are working with, and the actual code you're using to read it in/sort it, etc. – Jonathon Reinhart Nov 25 '13 at 02:10
  • Again, it would be a list of pairs (2-tuples). – Jonathon Reinhart Nov 25 '13 at 02:15
  • Finally, I would probably delete this question. It completely misrepresents the real problem, and the *actual* answer is buried in comments on my answer, based on pure (luckily correct) speculation on my part. Since we didn't have your code that shows the problem, we couldn't show code that fixes the problem. In other words, this will only serve to confuse future visitors. – Jonathon Reinhart Nov 25 '13 at 02:17

2 Answers2

1

So close!

sorted_list = sorted(list_not_sorted, key=lambda x:x[1], reverse=True)

x[2] is out of bounds (as your inner lists only have 2 items, and this is accessing the third). Since you want to key off the number, you want x[1].

>>> data = [['John', 973], ['Jim', 99], ['Jason', 912345]]
>>>
>>> sorted(data, key=lambda x:x[1], reverse=True)
[['Jason', 912345], ['John', 973], ['Jim', 99]]
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Sorry about the confusion. I've edited the question to make it more clear. :) – Web Hopeful Nov 25 '13 at 01:50
  • What other approach could I use? Essentially what I have is a ton of data points in a list and need to find the most popular in it. I thought sorting it would be easiest so I could just grab the top ones ( the most popular ). – Web Hopeful Nov 25 '13 at 01:55
  • @user3024130 I've deleted the previous comments. Now, my / your code works as intended. What is the problem? – Jonathon Reinhart Nov 25 '13 at 02:00
  • That is very strange. I have used that code and getting this output still: https://www.dropbox.com/s/jcr7mo37drlolgc/Screenshot%202013-11-24%2021.05.44.png – Web Hopeful Nov 25 '13 at 02:06
  • 1
    @user3024130 Probably because your data has *strings* for the second element, where our test code is using `ints`. Make sure to convert to `int`. – Jonathon Reinhart Nov 25 '13 at 02:07
  • Okay that makes sense Jonathon. I will try that now. – Web Hopeful Nov 25 '13 at 02:10
-1

You don't need to create a copy of your data using sorted. sort is enough.

>>> l
[['John', 973], ['Jim', 99], ['Jason', 912345]]
>>> l.sort(key=lambda x:x[1], reverse=True)
>>> l
[['Jason', 912345], ['John', 973], ['Jim', 99]]

If you need lexicographic order:

>>> l.sort(key=lambda x:str(x[1]), reverse=True)
Dimitris Leventeas
  • 1,622
  • 2
  • 16
  • 29