2

I'd like to know how to concatenate part of a string and sort it.

For example, i have a FIELD1 (string) and i should have the numbers of the string separated with ';' in the FIELD2. Only the numbers are repeated in the records. Moreover, i should sort the numbers ascending about their values.

enter image description here

enter image description here

How do I create an array string, in the field calculator of QGIS, to do this?

1 Answer 1

2

You need to create a new custom python expression in the Field Calculator to extract the numbers and sort them the way you want.

You can use the following expression:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def extract_digits(text_field, feature, parent):
    split_text = text_field.split()
    num = [int(text) for text in split_text if text.isdigit()]
    num_sort = sorted(num)
    join_sort = ';'.join(['{}'.format (i,) for i in num_sort])
    return join_sort

enter image description here

Then call the function from the custom expression, and use the field name that contains the street names:

enter image description here

Do not forget to select the new field in which the sorted digits will be saved.

The output will be like this:

enter image description here

Update

Based on your comment, I updated the script to take into consideration a semicolon (;) that comes directly after a number. Please use the following code:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def extract_digits(text_field, feature, parent):
    split_text = text_field.split(';')
    merge_text = ' '.join(split_text)
    split_text = merge_text.split()
    num = [int(text) for text in split_text if text.isdigit()]
    num_sort = sorted(num)
    join_sort = ';'.join(['{}'.format (i,) for i in num_sort])
    return join_sort
5
  • Thank you very much! I'll try, i'm sure that it will works. If i already have the "FIELD2" with random numbers separated with ';' , how can i sort these, with ascending values, in a new field? Should i use array_sort? thanks..
    – Mark
    Jul 18, 2020 at 11:12
  • @Mark in your case use the above code to create custom expression. Then check Update existing field and select FIELD2, from the custom expression select extract_digits(“FIELD1”). The numbers will be sorted in ascending order by the code.
    – ahmadhanb
    Jul 18, 2020 at 11:45
  • ahmadhanb the script doesn't work if there is the number before the ';' (for example ...;avenue 105;...). It doesn't return the number 105 in the list. Is it possible?
    – Mark
    Jul 25, 2020 at 13:01
  • @Mark Please use the updated code.
    – ahmadhanb
    Jul 27, 2020 at 0:40
  • Ok, Thank you very much!!
    – Mark
    Jul 31, 2020 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.