3

I would like to order my field like "normal" but as this column is string it won't work. I got "Emplacement réservé 105";"Emplacement réservé 106"; .... before "Emplacement réservé 20".

What should I do? to have Emplacement réservé 10 Emplacement réservé 25 Emplacement réservé 99 Emplacement réservé 105 Emplacement réservé 105

Exemple

3 Answers 3

7

Since QGIS 2.16 you can sort by whatever expression you'd like.

Right click the attribute table header, click on Sort and enter the expression you want.

E.g. extract the interesting part with a regular expression

to_int(regexp_substr("your_column", '.*(\\d+)'))

enter image description here

In earlier versions of QGIS (2.14 LTR) the simplest way would be to create a new virtual field with the above expression and use this for sorting.

7
  • This is rather cool. Could you explain this bit please, is it python or something? '.*(\\d+)' Commented Oct 20, 2016 at 11:09
  • 1
    Regular expressions are a swiss army knife for pattern matching. The syntax which is used here is described in the QRegExp help. Commented Oct 20, 2016 at 11:11
  • Thx this is great for the project, but what I need do for the layout section? This sheet as to appear with the "right" sorting in the layout section
    – Arthur
    Commented Oct 20, 2016 at 12:14
  • Are you referring to the "print composer"? Commented Oct 20, 2016 at 12:23
  • Yes I am. In the print composer I added an attribute table for this layer.
    – Arthur
    Commented Oct 20, 2016 at 12:50
0

It's because it's a string so QGIS sorts it character by character. You have to extract the number as a integer (in your case) with the field calculator and then you will be able to sort it as you want, using something like:

replace("TXT", 'ER ', '')

Cheers,

0

You could create a new column in the table and using the field calculator cut the number out of the string using right() orleft() (You can use one inside the other).

If you set the column to type:integer then you don't have to worry about trailing spaces you may have picked up with the difference between 2 and 3 digit numbers, but you won't get a numerically ordered list as QGIS likes to number (e.g: 1,2,3,4,5,27,45) in a weird manner (1,2,27,3,4,45,5). Retain your new column a type:text and a zero to the start of you smaller number (01, 02, 03) and that should then be ordered (e.g: 01,02,03,04,05,27,45).

The left image is an ordered text column, the right image is an order integer column.

enter image description here

You could create a new column in the table and using the field calculator cut the number out of the string using right() orleft() (You can use one inside the other).

If you set the column to type:integer then you don't have to worry about trailing spaces you may have picked up with the difference between 2 and 3 digit numbers, but you won't get a numerically ordered list as QGIS likes to number (e.g: 1,2,3,4,5,27,45) in a weird manner (1,2,27,3,4,45,5). Retain your new column a type:text and a zero to the start of you smaller number (01, 02, 03) and that should then be ordered (e.g: 01,02,03,04,05,27,45).

The left image is an ordered text column, the right image is an order integer column.

enter image description here

UPDATE

If it's you 'TXT' column you are trying to order by then run these expressions to update the column:

trim(substr("TXT",4,3))

This extracts just the number and clean any spaces from the string.

CASE WHEN length("TXT") < 3 THEN '0' + "TXT" ELSE "TXT" END 

This will check for any number under 3 digits and add a 0 to the front, leaving the 3 digit numbers alone

Your Answer

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

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