Sorting
{
"data": ... ,
"mark": ... ,
"encoding": {
"x": {
"field": ...,
"type": ...,
"sort": ..., // sort
...
},
"y": ...,
...
},
...
}
sort
property of a mark properties channel determines the order of the scale domain. Supported sort
value depends on the field’s scale type.
Continuous Scale
If the channel has a continuous scale (quantitative or time), sort
can have the following values:
"ascending"
– the field is sort by the field’s value in ascending order."descending"
– the field is sort by the field’s value in descending order.
Default value: "ascending"
.
Example: Reversed X-Scale
Setting x’s sort
to "descending"
reverses the x-axis. Thus, the following visualization’s x-axis starts on the maximum value of the field “Horsepower” and ends on zero.
Ordinal Scale
If the channel has an ordinal scale, the field’s values of the channel can be sorted in the following ways:
1) Sorting by the values’s natural order in Javascript. For example, "a"
< "b"
. In this case, sort
can be:
"ascending"
– sort by the field’s value in ascending order."descending"
– sort by the field’s value in descending order.
2) Sorting by aggregated value of another “sort” field. In this case, sort
is a sort field definition object, which has the following properties:
Property | Type | Description |
---|---|---|
field | Field | The field name to aggregate over. |
op | String | A valid aggregation operation (e.g., mean , median , etc.). |
order | String | "ascending" or "descending" order. |
3) Unsorted – "none
” – The field is not sorted. This is equivalent to specifying sort: false
in Vega’s scales.
Default value: "ascending"
.
4) Specify custom order by providing custom scale
’s domain
. (In this case, you don’t need to use sort
property.)
Example: Sorting Ordinal Scale by Another Field
The following example sorts x by mean of Horsepower.