Add an option to sort numerically.

This commit is contained in:
Tommy Goode 2019-11-16 03:21:28 -06:00 committed by Thomas Lovén
parent 42315226bd
commit acf8fd1be1
3 changed files with 10 additions and 2 deletions

View File

@ -158,11 +158,13 @@ sort:
attribute: <attribute>
first: <first>
count: <count>
numeric: <numeric>
```
- `method:` **Required** One of `domain`, `entity_id`, `name`, `state` or `attribute`
- `reverse:` Set to `true` to reverse the order. Default: `false`.
- `ignore_case:` Set to `true` to make the sort case-insensitive. Default: `false`.
- `numeric:` Set to `true` to sort by numeric value. Default: `false`.
- `attribute:` Attribute to sort by if `method: attribute`. Can be an *object attribute* as above (e.g. `attribute: rgb_color:2`)
- `first` and `count` can be used to only display `<count>` entities, starting with the `<first>` (starts with 0).

File diff suppressed because one or more lines are too long

View File

@ -16,6 +16,12 @@ export function entity_sorter(hass, method) {
function compare(_a, _b) {
if(method.ignore_case && _a.toLowerCase) _a = _a.toLowerCase();
if(method.ignore_case && _b.toLowerCase) _b = _b.toLowerCase();
if(method.numeric) {
if (!(isNaN(parseFloat(_a)) && isNaN(parseFloat(_b)))) {
_a = isNaN(parseFloat(_a)) ? undefined : parseFloat(_a);
_b = isNaN(parseFloat(_b)) ? undefined : parseFloat(_b);
}
}
if(_a === undefined && _b === undefined) return 0;
if(_a === undefined) return lt;
if(_b === undefined) return gt;