0

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.

1
  • that shouldn't really make much difference for sorting. what exactly are you trying to do? You should show us what you've got and where your problems are. Commented Nov 20, 2012 at 16:51

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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]

4 Comments

Doesn't have to be a tuple. sort can take any key or cmp
@inspectorG4dget I like your solution, but putting it in a tuple and using string formatting to print feels like the path of least resistance-- having data in a format that doesn't like to be sorted, therefore eureka, i'll make a custom sort function, seems like it just complicates things.
I agree that changing the data-structure would be the ideal way to go about doing this. But if this is part of a bigger program, that might not be an option. Also, lookup the optional maxsplit argument in str.split.
@inspectorG4dget Yeah, that's cleaner than using indexing. I'll edit it.

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.