Skip to content

Commit

Permalink
feat(lib::library_db): sort found entries AlphaNumerically
Browse files Browse the repository at this point in the history
fixes #417
  • Loading branch information
hasezoey committed Jan 27, 2025
1 parent 0cf4260 commit 2b5f6f1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/src/library_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,22 @@ impl DataBase {
let conn = self.conn.lock();
let mut stmt = conn.prepare(&search_str)?;

let mut vec_records: Vec<TrackDB> = stmt
let mut vec_records: Vec<(String, TrackDB)> = stmt
.query_map([criteria_val], TrackDB::try_from_row_named)?
.flatten()
.map(|v| (get_pin_yin(&v.name), v))
.collect();

// Left for debug
// error!("criteria_val: {}", criteria_val);
// error!("criteria: {}", criteria);
// error!("vec: {:?}", vec_records);

vec_records.sort_by_cached_key(|k| get_pin_yin(&k.name));
// TODO: if SearchCriteria is "Album", maybe we should sort by album track index
// TODO: should we really do the search here in the libary?
vec_records.sort_by(|a, b| alphanumeric_sort::compare_str(&a.0, &b.0));

let vec_records = vec_records.into_iter().map(|v| v.1).collect();
Ok(vec_records)
}

Expand All @@ -266,15 +271,20 @@ impl DataBase {
let conn = self.conn.lock();
let mut stmt = conn.prepare(&search_str)?;

let mut vec: Vec<String> = stmt
// tuple.0 is the sort key, and tuple.1 is the actual value
let mut vec: Vec<(String, String)> = stmt
.query_map([], |row| {
let criteria: String = row.get(0)?;
Ok(criteria)
})?
.flatten()
.map(|v| (get_pin_yin(&v), v))
.collect();

vec.sort_by_cached_key(|k| get_pin_yin(k));
// TODO: should we really do the search here in the libary?
vec.sort_by(|a, b| alphanumeric_sort::compare_str(&a.0, &b.0));

let vec = vec.into_iter().map(|v| v.1).collect();
Ok(vec)
}

Expand Down

0 comments on commit 2b5f6f1

Please sign in to comment.