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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 16, 2020 at 21:50

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
answered Jul 17, 2020 at 2:00
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.. Commented 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. Commented 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? Commented Jul 25, 2020 at 13:01
  • @Mark Please use the updated code. Commented Jul 27, 2020 at 0:40
  • Ok, Thank you very much!! Commented Jul 31, 2020 at 11:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.