convert asc to desc instead of appending desc to the end of the order

as the latter doesn't work when sorting on more than one column

https://anki.tenderapp.com/discussions/beta-testing/1868-anki-2124-beta#comment_48174812
This commit is contained in:
Damien Elmes 2020-03-23 19:53:57 +10:00
parent 84eaf43525
commit 11a4d582b4
2 changed files with 30 additions and 18 deletions

View File

@ -617,10 +617,16 @@ where c.nid = n.id and c.id in %s group by nid"""
# if order=True, use the sort order stored in the collection config
# if order=False, do no ordering
# if order is a string, that text is added after 'order by' in the sql statement
# if order is an int enum, sort using that builtin sort.
#
# the reverse argument only applies when a BuiltinSortKind is provided.
# if order is a string, that text is added after 'order by' in the sql statement.
# you must add ' asc' or ' desc' to the order, as Anki will replace asc with
# desc and vice versa when reverse is set in the collection config, eg
# order="c.ivl asc, c.due desc"
#
# if order is an int enum, sort using that builtin sort, eg
# col.find_cards("", order=BuiltinSortKind.CARD_DUE)
# the reverse argument only applies when a BuiltinSortKind is provided;
# otherwise the collection config defines whether reverse is set or not
def find_cards(
self, query: str, order: Union[bool, str, int] = False, reverse: bool = False,
) -> Sequence[int]:

View File

@ -61,29 +61,35 @@ pub(crate) fn search_cards<'a, 'b>(
fn write_order(sql: &mut String, kind: &SortKind, reverse: bool) -> Result<()> {
let tmp_str;
let order = match kind {
SortKind::NoteCreation => "n.id, c.ord",
SortKind::NoteMod => "n.mod, c.ord",
SortKind::NoteField => "n.sfld collate nocase, c.ord",
SortKind::CardMod => "c.mod",
SortKind::CardReps => "c.reps",
SortKind::CardDue => "c.type, c.due",
SortKind::NoteCreation => "n.id asc, c.ord asc",
SortKind::NoteMod => "n.mod asc, c.ord asc",
SortKind::NoteField => "n.sfld collate nocase asc, c.ord asc",
SortKind::CardMod => "c.mod asc",
SortKind::CardReps => "c.reps asc",
SortKind::CardDue => "c.type asc, c.due asc",
SortKind::CardEase => {
tmp_str = format!("c.type = {}, c.factor", CardType::New as i8);
tmp_str = format!("c.type = {} asc, c.factor asc", CardType::New as i8);
&tmp_str
}
SortKind::CardLapses => "c.lapses",
SortKind::CardInterval => "c.ivl",
SortKind::NoteTags => "n.tags",
SortKind::CardDeck => "(select v from sort_order where k = c.did)",
SortKind::NoteType => "(select v from sort_order where k = n.mid)",
SortKind::CardTemplate => "(select v from sort_order where k1 = n.mid and k2 = c.ord)",
SortKind::CardLapses => "c.lapses asc",
SortKind::CardInterval => "c.ivl asc",
SortKind::NoteTags => "n.tags asc",
SortKind::CardDeck => "(select v from sort_order where k = c.did) asc",
SortKind::NoteType => "(select v from sort_order where k = n.mid) asc",
SortKind::CardTemplate => "(select v from sort_order where k1 = n.mid and k2 = c.ord) asc",
};
if order.is_empty() {
return Ok(());
}
sql.push_str(order);
if reverse {
sql.push_str(" desc");
sql.push_str(
&order
.to_ascii_lowercase()
.replace(" desc", "")
.replace(" asc", " desc"),
)
} else {
sql.push_str(order);
}
Ok(())
}