g3w-suite / g3w-admin

Server module for G3W-SUITE
https://g3w-suite.readthedocs.io/en/latest/g3wsuite_administration.html
Mozilla Public License 2.0
39 stars 31 forks source link

Sort formatted values in vector API response (`ordering` + `fformatter` params) #806

Closed Raruto closed 2 months ago

Raruto commented 2 months ago

Checklist

Motivation

Be able to sort the the following values by "formatted" field value instead of by field key:

https://v37.g3wsuite.it/vector/api/data/qdjango/97/buildings_2f43dc1d_6725_42d2_a09b_dd446220104a/?fformatter=address&ordering=address

// current behavior

"data": [
  [ "A101", "Via Sallustio Bandini" ],
  [ "A102", "Via Atto Vannucci" ],
  [ "A103", "Via Alfredo Oriani gia' Via Vecchia" ],
  [ "A106", "Piazza Pietro Leopoldo" ],
  [ "A107", "Via Ubaldo Montelatici" ],
  [ "A108", "Via Giandomenico Romagnosi" ],
  [ "B002", "Via Giovanni Lampredi" ],
  [ "C106", "Via Pietro Cironi" ],
  [ "C107", "Via Pietro Corridoni" ]
],

Suggested solution

Introduce a new parameter or change the ordering behavior so that response can be like following:

// desired behavior

"data": [
  [ "A106", "Piazza Pietro Leopoldo" ],
  [ "A103", "Via Alfredo Oriani gia' Via Vecchia" ],
  [ "A102", "Via Atto Vannucci" ],
  [ "A108", "Via Giandomenico Romagnosi" ],
  [ "B002", "Via Giovanni Lampredi" ],
  [ "C106", "Via Pietro Cironi" ],
  [ "C107", "Via Pietro Corridoni" ],
  [ "A101", "Via Sallustio Bandini" ],
  [ "A107", "Via Ubaldo Montelatici" ]
],

NB the sorting algo must also take into account numeric, null and boolean values (see below)

Alternatives considered

Formatted values can be sorted on client-side, but this make every API calls code even more complex/unreliable:

fetch('https://v37.g3wsuite.it/vector/api/data/qdjango/97/buildings_2f43dc1d_6725_42d2_a09b_dd446220104a/?fformatter=address' /* + '&ordering=address' */)
  .then(d => d.json())
  // sorted by fformatter
  .then(d => d.data.sort((a, b) => `${a[1]}`.localeCompare(b[1], undefined, 'number' === typeof a[1] ? { numeric: true } : { sensitivity: 'base' })))