Jagadish Paul
Jagadish Paul

Reputation: 3

python string array sorting

I want to sort string array using python.

I have a string array

str_array = {string1, string2, string3}
all string formats are AB-CD-EFGH-IJ-NAME ------> 12-34-5678-09-PHOTO

AB= 00 to 99
CD= 00 to 99
EFGH= 0000 to 9999
IJ= 00 to 99
NAME= any alphabetic name

I want sort string first with there equivalent numeric value (remove "-" from numeric part of string) then alphabetic order.

For example 12-34-5678-09-PHOTO will be 1234568809-PHOTO.

Upvotes: 0

Views: 816

Answers (3)

unutbu
unutbu

Reputation: 880897

Define a key function which takes an element of the iterable str_array as input, and returns a tuple key consisting of the integer (first) and name (second):

def mykey(text):
    numtxt, name = text.rsplit('-',1)
    numtxt = int(numtxt.replace('-',''))
    return (numtxt, name)

new_str = sorted(str_array, key = mykey)

For example,

In [30]: sorted(['12-34-5678-09-PHOTO', '12-34-5678-09-MOTOR', '12-04-5678-09-PHOTO', ], key = mykey)
Out[30]: ['12-04-5678-09-PHOTO', '12-34-5678-09-MOTOR', '12-34-5678-09-PHOTO']

For more on this sorting technique, see the HOWTO Sort wiki.


Note that sorted returns a new list. If you wish to sort str_array in-place, then use

str_array.sort(key = mykey)

Upvotes: 0

kreativitea
kreativitea

Reputation: 1791

Er, how is this sorting?

In any case.

"".join(yourstring.split('-')) 

is the canonical way to remove dashes. You can keep the last one by specifying the max number of splits.

"".join(test.split('-', 3))

If you want to sort by the alphabetic part, it's not going to work intuitively unless you use store just the alphabetic part in a tuple and sort by that key:

temp = test.split('-')
numbers, alpha = "".join(temp[:-1]), temp[-1]

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 114035

I don't fully understand your question, but see if this helps:

def myCmp(s1, s2):
    s1 = s1.replace('-', '', 3)
    s2 = s2.replace('-', '', 3)
    int1, _, name1 = s1.partition('-')[0]
    int2, _, name2 = s2.partition('-')[0]
    if int1 < int2:
        return -1
    elif int1 > int2:
        return 1
    elif name1 < name2:
        return -1
    elif name1 > name 2:
        return 1
    else:
        return 0

str_array.sort(cmp=myCmp)

Upvotes: 1

Related Questions