Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list of mixed strings based on digits

Tags:

python

How do I sort this list via the numerical values? Is a regex required to remove the numbers or is there a more Pythonic way to do this?

to_sort

['12-foo',
 '1-bar',
 '2-bar',
 'foo-11',
 'bar-3',
 'foo-4',
 'foobar-5',
 '6-foo',
 '7-bar']

Desired output is as follows:

1-bar
2-bar
bar-3
foo-4
foobar-5
6-foo
7-bar
foo-11
12-foo
like image 985
nipy Avatar asked Aug 24 '16 17:08

nipy


People also ask

Can you sort a list which contains both numeric and string data?

Definition and Usage Note: You cannot sort a list that contains BOTH string values AND numeric values.

How do you sort text and mixed numbers in Python?

Method #2 : Using sorted() + key + lambda + isdigit() The combination of above functionalities can also be used to achieve solution to this problem. In this, we just sort the list using sorted() using key functionality using lambda function to segregate digits using isdigit().

Can sort () sort strings?

The sort() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

How do I sort strings that contain numbers in Java?

Sort String in Java String in Java is immutable. There is no direct method to sort a string in Java. You can use Arrays, which has a method CharArray() that will create a char input string and using another method (Arrays.


2 Answers

One solution is the following regex extraction:

sorted(l, key=lambda x: int(re.search('\d+', x).group(0)))

>>> l
['12-foo', '1-bar', '2-bar', 'foo-11', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar']
>>> sorted(l, key=lambda x: int(re.search('\d+', x).group(0)))
['1-bar', '2-bar', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar', 'foo-11', '12-foo']

The key is the extracted digit (converted to int to avoid sorting lexographically).

like image 110
Maroun Avatar answered Oct 24 '22 01:10

Maroun


If you don't want to use regex

>>> l = ['12-foo', '1-bar', '2-bar', 'foo-11', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar']

>>> sorted(l, key = lambda x: int(''.join(filter(str.isdigit, x))))

['1-bar', '2-bar', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar', 'foo-11', '12-foo']
like image 4
Asish M. Avatar answered Oct 24 '22 03:10

Asish M.