4

How i can use python to sort the list format

format=["12 sheet","4 sheet","48 sheet","6 sheet", "busrear", "phonebox","train"]

like this way

format =["4 sheet", "6 sheet", "12 sheet", "48 sheet", "busrear", "phonebox", "train"]

whose answer is here Python sort array of string with integers inside

but If the array is a list of list then how can we do that like this one

format=[[1, '12 sheet', 0],[2, '4 sheet', 0], [3, '48 sheet', 0], [4, '6 sheet', 0 [5, 'Busrear', 0], [6, 'phonebox', 0], [7, 'train', 0]]

I Need the result to be like this

format=[[2, '4 sheet', 0],[4, '6 sheet', 0],[1, '12 sheet', 0],[3, '48 sheet', 0],[5, 'Busrear', 0], [6, 'phonebox', 0], [7, 'train', 0]]
7
  • if its list of list then you have to decide on which bases you have to sort? on first index base of list or other? Commented Feb 27, 2014 at 11:15
  • there inbuilt methods for lists like sort() which just use assemble ints first then deal with chars and so forth Commented Feb 27, 2014 at 11:16
  • Are you asking how to sort a list or how to sort a list of lists with sorting key inside the inner list? Commented Feb 27, 2014 at 11:18
  • I am just asking how to sort according to string present in each sub list Commented Feb 27, 2014 at 11:19
  • 2
    and why the long and unicode? Commented Feb 27, 2014 at 11:19

5 Answers 5

5

You can do this:

lst = [[1L, u'12 sheet', 0],
       [2L, u'4 sheet', 0],
       [3L, u'48 sheet', 0],
       [4L, u'6 sheet', 0],
       [5L, u'Busrear', 0],
       [6L, u'phonebox', 0],
       [7L, u'train', 0]]

def sortby(x):
    try:
        return int(x[1].split(' ')[0])
    except ValueError:
        return float('inf')

lst.sort(key=sortby)
print lst

Output:

[[2L, u'4 sheet', 0], [4L, u'6 sheet', 0], [1L, u'12 sheet', 0], [3L, u'48 sheet', 0], [5L, u'Busrear', 0], [6L, u'phonebox', 0], [7L, u'train', 0]]

You can always use fancier list comprehension but readability counts. Which is why you might not feel like modifying the cool solution by falsetru for this slightly changed task.

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

Comments

3

You can trivially sort a list of strings with built-in sorted method. Even if the objects in your list are more complex, you can still use sorted. Just pass a custom key parameter to use the second item from the inner list as the key in ordering comparisons:

result = sorted(format, key=lambda x: x[1])

Finally switch to your sorting function natsorted (from natsort package) and you end up with the desired, naturally sorted, result list:

from natsort import natsorted
result = natsorted(format, key=lambda x: x[1])

Comments

1

you can use sorted or list.sort

>>> format.sort(key=lambda x: (x[1]))
[[1L, u'12 sheet', 0], [2L, u'4 sheet', 0], [3L, u'48 sheet', 0], [4L, u'6 sheet', 0], [5L, u'Busrear', 0], [6L, u'phonebox', 0], [7L, u'train', 0]]

1 Comment

How is this sorted as per the requirement?
1

The same solution can be used here

print sorted(format_data, key=lambda x: (int(x[1].split(None, 1)[0]) if x[1][:1].isdigit() else 999, x))

Note the x[1] instead of just x. x[1] means the second element in x.

Result:

[[2, '4 sheet', 0],
 [4, '6 sheet', 0],
 [1, '12 sheet', 0],
 [3, '48 sheet', 0],
 [5, 'Busrear', 0],
 [6, 'phonebox', 0],
 [7, 'train', 0]]

Comments

0
a = [5,1,"a","A",2]
a = list(map(int,filter(lambda x:x.isdigit(),sorted(map(str,a)))))+list(filter(lambda x:x.isalpha(),sorted(map(str,a))))
print(a)

1 Comment

Add some explanation with answer for how this answer help OP in fixing current issue

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.