I have a field with names, a string field, and I want to create another one with serial content following the alphabetical order.
I have this:
|Field1|
| B |
| C |
| A |
| D |
And I want to reach this:
|Field1| Field2 |
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
The MMQGIS plugin offers a tool to sort Shapefiles, but is this operation not possible directly in the field calculator? I'm very interested to know how to make this to make a model in the graphical modeller
-
related answer for QGIS3geozelot– geozelot2018年04月12日 12:31:21 +00:00Commented Apr 12, 2018 at 12:31
1 Answer 1
As a workaround for QGIS 2.x, try
- Toolbox -> QGIS Geoalgorithms-> Execute SQL
- Additional input datasources -> choose your layer
- SQL Query ->
SELECT * FROM input1 ORDER BY Field1
(This creates a new temporary layer)
- Field Calculator -> Create a new field ->
@row_number
or
Layer -> Add Layer -> Add/Edit Virtual Layer
Import -> choose your layer
Query ->
SELECT * FROM <your_layer_name> ORDER BY Field1
add
@row_number
as above
-
...seems odd that I don't know a more elegant solution to this, despite working with QGIS for ages. anybody else?geozelot– geozelot2018年04月12日 13:14:53 +00:00Commented Apr 12, 2018 at 13:14