tags:

views:

24

answers:

1

Hi,

I have array of strings in following format 5_5_a, 10_10_a, 0_0_a 1_1_a etc. I want to sort them in ascending/descending order. When I use sortedArrayUsingSelector:@selector(compare:) I get 0_0_a,10_10_a,1_1_a,5_5_a. I tried to parse each string into 5 5 a chunks and cast to a int then put that first element into array with addObject:myInteger but i got warnings when compiling. Please let me know if I am missing something. Thanks Mark

A: 

This is because the ascii code of '_' is bigger than '0'.

I don't know if you can change '_' into '-' for example ? It will fix your problem and you will be able to use sortedArrayUsingSelector.

If not you can do :

[str stringByReplacingOccurancesOfString:@"_" withString:@"-"]

Then sort your array and finally replace your '-' with '_'.

(it is just a solution... not the best one ^^)

William Remacle
Thanks for your help, What I have done may have been not the most elegant solution, but I have parsed the string prepended 0 to the first part of parsed string and then sorted ascending/descending.
Mark