From 11a4d582b4824c4900ae2b1bb040f7b0c0d24a3f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 23 Mar 2020 19:53:57 +1000 Subject: [PATCH] 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 --- pylib/anki/collection.py | 12 +++++++++--- rslib/src/search/cards.rs | 36 +++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index 920cabfff..fedd21f76 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -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]: diff --git a/rslib/src/search/cards.rs b/rslib/src/search/cards.rs index 2c10f1e8d..8ab045b7b 100644 --- a/rslib/src/search/cards.rs +++ b/rslib/src/search/cards.rs @@ -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(()) }